formulas = FormulaModel::orderBy('size') ->get() ->groupBy('size') ->map(function ($items) { return $items ->map(fn ($item) => [ 'hash' => $item->hash, 'quality' => $item->quality, 'volume' => $item->volume, ]) ->sortBy('volume'); }); $this->sizes = Bottle::distinct()->orderBy('size')->pluck('size')->toArray(); } #[On('modal:open')] public function openModal(string $method, string $modalTitle, ?string $id = null): void { $this->resetValidation(); $this->resetErrorBag(); $this->method = $method; $this->modalTitle = $modalTitle; if ($id) { $this->form->setFormula(FormulaModel::byHashOrFail($id)); } } public function create(): void { $this->canOrAbort('create formula'); if (FormulaModel::where('quality', $this->form->quality)->where('size', $this->form->size)->exists()) { $this->toast('Rumus dengan kualitas dan ukuran tersebut sudah ada.', 'Info', 'warning'); return; } $formula = $this->form->store(); // push new formula to collection $size = $formula->size; $newFormula = [ 'hash' => $formula->hash, 'quality' => $formula->quality, 'volume' => $formula->volume, ]; // push new formula to collection $size = $formula->size; $newFormula = [ 'hash' => $formula->hash, 'quality' => $formula->quality, 'volume' => $formula->volume, ]; // Recreate the collection instead of modifying in place $newFormulas = $this->formulas->toArray(); if (! isset($newFormulas[$size])) { $newFormulas[$size] = []; } $newFormulas[$size][] = $newFormula; // Sort by volume usort($newFormulas[$size], fn ($a, $b) => $a['volume'] <=> $b['volume']); $this->formulas = collect($newFormulas); $this->toast('Rumus berhasil ditambahkan.'); Flux::modals()->close(); } public function update(): void { $this->canOrAbort('update formula'); $exists = FormulaModel::where('quality', $this->form->quality) ->where('size', $this->form->size) ->where('id', '!=', $this->form->formula->id) ->exists(); if ($exists) { $this->toast('Rumus dengan kualitas dan ukuran tersebut sudah ada.', 'Info', 'warning'); return; } $formula = $this->form->update(); // update collection formulas $newFormulas = $this->formulas->toArray(); foreach ($newFormulas as $size => &$items) { foreach ($items as &$item) { if ($item['hash'] === $formula->hash) { $item = [ 'hash' => $formula->hash, 'quality' => $formula->quality, 'volume' => $formula->volume, ]; break; } } // Sort by volume usort($items, fn ($a, $b) => $a['volume'] <=> $b['volume']); } unset($items); $this->formulas = collect($newFormulas); $this->toast('Rumus berhasil diperbarui.'); Flux::modals()->close(); } public function delete(FormulaModel $formula): void { $formula->delete(); // update collection formulas $size = $formula->size; $newFormulas = $this->formulas->toArray(); if (isset($newFormulas[$size])) { $newFormulas[$size] = array_values(array_filter($newFormulas[$size], fn ($item) => $item['hash'] !== $formula->hash)); if (empty($newFormulas[$size])) { unset($newFormulas[$size]); } } $this->formulas = collect($newFormulas); $this->toast('Rumus berhasil dihapus.'); Flux::modals()->close(); } public function render(): View { return view('livewire.studio.master.formulas', [ 'pageTitle' => 'Rumus', ]); } }