diff --git a/app/Livewire/Studio/Master/Formula.php b/app/Livewire/Studio/Master/Formula.php index 01f51d6..2cb5c7c 100644 --- a/app/Livewire/Studio/Master/Formula.php +++ b/app/Livewire/Studio/Master/Formula.php @@ -24,7 +24,7 @@ class Formula extends Component public FormulaForm $form; - public $formulas; + public array $formulas = []; public string $method = 'create'; @@ -48,8 +48,9 @@ public function mount(): void ]) ->sortBy('volume') ->values() - ->all() - ); + ->toArray() + ) + ->toArray(); $this->sizes = Bottle::distinct()->orderBy('size')->pluck('size')->toArray(); } @@ -74,16 +75,20 @@ public function create(): void 'volume' => $formula->volume, ]; - // Get the group for this size, or an empty Collection - $group = $this->formulas->get($size, collect()); + // Convert to collection for manipulation + $formulas = collect($this->formulas); + + // Get the group for this size, or an empty collection + $group = collect($formulas->get($size, [])); // Add, then re-sort $group = $group->push($newFormula) ->sortBy('volume') - ->values(); + ->values() + ->toArray(); - // Put it back - $this->formulas->put($size, $group); + // Put it back and convert to array + $this->formulas = $formulas->put($size, $group)->toArray(); $this->toast('Rumus berhasil ditambahkan.'); @@ -107,20 +112,23 @@ public function update(): void $formula = $this->form->update(); - // update array formulas - $this->formulas = $this->formulas->map(function ($group) use ($formula) { - return collect($group) - ->map(function ($item) use ($formula) { - if ($item['hash'] === $formula->hash) { - $item['quality'] = $formula->quality; - $item['volume'] = $formula->volume; - } + // Convert to collection for manipulation + $this->formulas = collect($this->formulas) + ->map(function ($group) use ($formula) { + return collect($group) + ->map(function ($item) use ($formula) { + if ($item['hash'] === $formula->hash) { + $item['quality'] = $formula->quality; + $item['volume'] = $formula->volume; + } - return $item; - }) - ->sortBy('volume') - ->values(); - }); + return $item; + }) + ->sortBy('volume') + ->values() + ->toArray(); + }) + ->toArray(); $this->toast('Rumus berhasil diperbarui.'); @@ -133,22 +141,26 @@ public function delete(FormulaModel $formula): void $formula->delete(); - // update array formulas + // Convert to collection for manipulation $size = $formula->size; + $formulas = collect($this->formulas); - if ($this->formulas->has($size)) { - $group = collect($this->formulas->get($size)) + if ($formulas->has($size)) { + $group = collect($formulas->get($size)) ->reject(fn ($item) => $item['hash'] === $formula->hash) ->values() - ->all(); + ->toArray(); if (empty($group)) { - $this->formulas->forget($size); + $formulas->forget($size); } else { - $this->formulas->put($size, $group); + $formulas->put($size, $group); } } + // Convert back to array + $this->formulas = $formulas->toArray(); + $this->toast('Rumus berhasil dihapus.'); Flux::modals()->close(); diff --git a/resources/views/livewire/studio/master/formulas.blade.php b/resources/views/livewire/studio/master/formulas.blade.php index 7ed2618..4c29589 100644 --- a/resources/views/livewire/studio/master/formulas.blade.php +++ b/resources/views/livewire/studio/master/formulas.blade.php @@ -22,7 +22,7 @@