From 7d45e3027c8a188a69ad18ac681e4d4570472a91 Mon Sep 17 00:00:00 2001 From: Malek Tellissi Date: Sun, 13 Sep 2026 00:14:30 +0200 Subject: [PATCH] Fix SpiceChart: use class property for spice IDs, build edit URLs via url() Co-Authored-By: Claude Sonnet 4.6 --- .../SpiceResource/Widgets/SpiceChart.php | 179 +++++++++--------- 1 file changed, 86 insertions(+), 93 deletions(-) diff --git a/app/Filament/User/Resources/SpiceResource/Widgets/SpiceChart.php b/app/Filament/User/Resources/SpiceResource/Widgets/SpiceChart.php index 9bfca6a..b1b05c2 100644 --- a/app/Filament/User/Resources/SpiceResource/Widgets/SpiceChart.php +++ b/app/Filament/User/Resources/SpiceResource/Widgets/SpiceChart.php @@ -6,103 +6,96 @@ use Leandrocfe\FilamentApexCharts\Widgets\ApexChartWidget; use Filament\Support\RawJs; use App\Models\Spice; -class SpiceChart extends ApexChartWidget { - protected static ?string $chartId = 'spiceChart'; - protected static ?string $heading = 'Rohstoffmengen im Lager'; +class SpiceChart extends ApexChartWidget +{ + protected static ?string $chartId = 'spiceChart'; + protected static ?string $heading = 'Rohstoffmengen im Lager'; - protected function getOptions(): array { - $data = Spice::all(); + private array $spiceIds = []; - $data_array = []; - $labels = []; - foreach ($data as $label) { - if ($label->unit == 'grams') { - $data_array[] = round($label->quantity / 1000, 2); - $labels[] = $label->name . ' (kg)'; - } else { - $data_array[] = $label->quantity; - if ($label->unit == 'pieces') { - $labels[] = $label->name . ' (Stück)'; - } elseif ($label->unit == 'liter') { - $labels[] = $label->name . ' (Liter)'; - } else { - $labels[] = $label->name . ' (' . ucfirst($label->unit) . ')'; - } - } - } - - - - return [ - 'chart' => [ - 'type' => 'bar', - 'height' => 300, - ], - 'series' => [ - [ - 'name' => 'BasicBarChart', - 'data' => $data_array, - ], - ], - 'xaxis' => [ - 'categories' => $labels, - 'labels' => [ - 'style' => [ - 'fontFamily' => 'inherit', - ], - ], - ], - 'yaxis' => [ - 'labels' => [ - 'style' => [ - 'fontFamily' => 'inherit', - ], - ], - ], - 'colors' => ['#f59e0b'], - 'plotOptions' => [ - 'bar' => [ - 'borderRadius' => 3, - 'horizontal' => false, - ], - ], - ]; - } - - protected function extraJsOptions(): ?RawJs { - $urls = \App\Models\Spice::all() - ->map(fn ($s) => \App\Filament\User\Resources\SpiceResource::getUrl('edit', ['record' => $s->id])) - ->values() - ->toJson(); - - return RawJs::make(<<spiceIds[] = $spice->id; + + if ($spice->unit === 'grams') { + $data[] = round($spice->quantity / 1000, 2); + $labels[] = $spice->name . ' (kg)'; + } elseif ($spice->unit === 'pieces') { + $data[] = $spice->quantity; + $labels[] = $spice->name . ' (Stück)'; + } elseif ($spice->unit === 'liter') { + $data[] = $spice->quantity; + $labels[] = $spice->name . ' (Liter)'; + } else { + $data[] = $spice->quantity; + $labels[] = $spice->name . ' (' . ucfirst($spice->unit) . ')'; } } + + return [ + 'chart' => [ + 'type' => 'bar', + 'height' => 300, + ], + 'series' => [ + [ + 'name' => 'Bestand', + 'data' => $data, + ], + ], + 'xaxis' => [ + 'categories' => $labels, + 'labels' => ['style' => ['fontFamily' => 'inherit']], + ], + 'yaxis' => [ + 'labels' => ['style' => ['fontFamily' => 'inherit']], + ], + 'colors' => ['#f59e0b'], + 'plotOptions' => [ + 'bar' => ['borderRadius' => 3, 'horizontal' => false], + ], + ]; + } + + protected function extraJsOptions(): ?RawJs + { + $urls = collect($this->spiceIds) + ->map(fn ($id) => url('/dashboard/rohstoffe/' . $id . '/edit')) + ->values() + ->toJson(); + + return RawJs::make(<<