<?php
namespace App\Livewire;
use....
class MenuComponent extends Component implements HasForms, HasActions
{
use InteractsWithForms;
use InteractsWithActions;
public $weeks;
public Week $week;
public ?ButteryMenu $butteryMenu;
public function mount($week = null): void
{
$this->week = $week ?? Week::where('menu_type', 'Buttery')->first();
$this->weeks = Week::where('menu_type', 'Buttery')->get();
$this->form->fill();
}
public function changeWeek($weekId)
{
$this->week = Week::find($weekId);
}
private array $sittingOrder = ['Breakfast', 'Brunch', 'Lunch', 'Dinner'];
private array $courseOrder = ['Starter', 'Mains', 'Sides', 'Dessert'];
public function editAction(): Action
{
return
Action::make('edit')
->fillForm(fn($livewire): array => [
....
'title' => $livewire->ownerRecord->title,
])
->form(ButteryMenuResource::getFormSchema())
->action(function (array $data, array $arguments): void {
$record = ButteryMenu::find($arguments['record']->id);
$record->update($data);
$this->dispatch('buttery-menu-saved');
});
}
public function createAction(): Action
{
return Action::make('create')...
}
public function getGroupedMenusProperty(): array
{
$menus = $this->week->butteryMenu;
return $this->groupAndSortMenus($menus)->all();
}
private function groupAndSortMenus(Collection $menus): Collection
{
return $menus->groupBy(......)->sortKeys();
}
public function render()
{
return view('livewire.menu-component')
->with('groupedMenus', $this->groupedMenus)
->with('weeks', $this->weeks)
->layout('menu');
}
}