laraveleloquentlaravel-filamentlaravel-11filamentphp

why widget didn't appear in panel?


I created chart widget for data_log for my project, at my first code i created this code for Volume PerMonth. In my database there's users, metergas, logs. In column metergas there are serialNo, connectivity,and user_id. In column logs there are id, condition,volume, type_io,battery, and metergas_id. So, here's my LogAppChart.php (dailyMonth):

<?php

namespace App\Filament\Widgets;

use App\Models\Log;
use App\Models\Metergas;
use Filament\Widgets\ChartWidget;
use Illuminate\Support\Facades\Auth;
use Carbon\Carbon;
use Carbon\CarbonPeriod;
use Illuminate\Support\Str; // To use the random color function

class LogAppChart extends ChartWidget
{
    protected static ?string $heading = 'Volume PerMonth';
    protected static ?string $pollingInterval = null;
    protected static ?string $maxHeight = '300px';
    protected int | string | array $columnSpan = 'full';
    protected function getData(): array
    {
        $user_id = Auth::user()->id;
        $metergas = Metergas::where('user_id', $user_id)
            ->get();

        $data_log = [];
        $period = CarbonPeriod::create(Carbon::now()->subMonth()->startOfDay(), '1 day', Carbon::now()->startOfDay());
        $allDates = iterator_to_array($period);

        $allDates = array_map(fn ($date) => $date->format('Y-m-d'), $allDates);

        if ($metergas->isNotEmpty()) {
            foreach ($metergas as $item) {
                $logs = Log::where('metergas_id', $item->id)
                    ->whereDate('created_at', '>=', Carbon::now()->subMonth())
                    ->get()
                    ->groupBy(function ($date) {
                        return Carbon::parse($date->created_at)->format('Y-m-d');
                    })
                    ->map(function ($dayLogs) {
                        return $dayLogs->sum('volume');
                    });

                $dailyData = [];
                foreach ($allDates as $date) {
                    $dailyData[] = $logs->get($date, 0);
                }

                // Generate a random color for each dataset
                $color = sprintf('#%06X', mt_rand(0, 0xFFFFFF));

                $data_log[] = [
                    'label' => 'Metergas ' . $item->serialNo,
                    'data' => $dailyData,
                    'backgroundColor' => $color,
                    'borderColor' => $color,
                ];
            }
        }

        return [
            'datasets' => $data_log,
            'labels' => $allDates,
        ];
    }

    protected function getType(): string
    {
        return 'line';
    }
}

I created another chart with similar code but its Volume PerDay. So, it count volume in every hour each day. but, the code chart widget didn't appear in my panel. enter image description here

<?php

namespace App\Filament\Widgets;

use App\Models\Log;
use App\Models\Metergas;
use Filament\Widgets\ChartWidget;
use Illuminate\Support\Facades\Auth;
use Carbon\Carbon;
use Carbon\CarbonPeriod;
use Illuminate\Support\Str; // To use the random color function

class DayLogAppChart extends ChartWidget
{
    protected static ?string $heading = 'Volume PerDay';
    protected static ?string $pollingInterval = null;
    protected static ?string $maxHeight = '300px';
    protected int | string | array $columnSpan = 'full';
    protected function getData(): array
    {
        $user_id = Auth::user()->id;
        $metergas = Metergas::where('user_id', $user_id)
            ->get();

        $data_log = [];
        $period = CarbonPeriod::create(Carbon::now()->subDay()->startOfHour(), '1 hour', Carbon::now()->startOfHour());
        $allHours = iterator_to_array($period);

        $allHours = array_map(fn ($date) => $date->format('Y-m-d H:00'), $allHours);

        if ($metergas->isNotEmpty()) {
            foreach ($metergas as $item) {
                $logs = Log::where('metergas_id', $item->id)
                    ->whereDate('created_at', '>=', Carbon::now()->subDay())
                    ->get()
                    ->groupBy(function ($log) {
                        return Carbon::parse($log->created_at)->format('Y-m-d H:00');
                    })
                    ->map(function ($hourLogs) {
                        return $hourLogs->sum('volume');
                    });

                    foreach ($allHours as $hourLabel) {
                        $hourlyData[] = $logs->get($hourLabel, 0);
                }

                // Generate a random color for each dataset
                $color = sprintf('#%06X', mt_rand(0, 0xFFFFFF));

                $data_log[] = [
                    'label' => 'Metergas ' . $item->serialNo,
                    'data' => $hourlyData,
                    'backgroundColor' => $color,
                    'borderColor' => $color,
                ];
            }
        }
        // dd($data_log, $allHours); // Uncomment for debugging
        return [
            'datasets' => $data_log,
            'labels' => $allHours,
        ];
    }

    protected function getType(): string
    {
        return 'line';
    }
}

Solution

  • It's possible you haven't added this new Widget to your Dashboard, in app/Providers/Filament/AdminPanelProvider.php.

    public function panel(Panel $panel): Panel
    {
        return $panel
            // ...
            ->widgets([
                // your Dashboard Widgets go here
            ]);
    }