How to pass data to an EditAction

I'm struggling to make the editAction to recieve the record where I call the action in the livewire blade component In blade i have : {{ ($this->editAction)(['record' => $menu]) }} component:
class MenuComponent extends Component implements HasForms
public function editAction(): Action
{
return
Action::make('edit')
->fillForm(fn(ButteryMenu $record): array => [

'title' => $record->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');
});
}
class MenuComponent extends Component implements HasForms
public function editAction(): Action
{
return
Action::make('edit')
->fillForm(fn(ButteryMenu $record): array => [

'title' => $record->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');
});
}
Do i need anything in the mount() ? (I have a similar createAction in the compoenent that's working fine )
8 Replies
toeknee
toeknee3mo ago
class MenuComponent extends Component implements HasForms
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');
});
}
class MenuComponent extends Component implements HasForms
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');
});
}
Maybe livewire ownerRecord?
RuiAlmeida
RuiAlmeidaOP3mo ago
It's complaining that the $record is null. is this the correct way to pass that to the action? {{ ($this->editAction)(['record' => $menu]) }} or is there any other way maybe with wire:click
toeknee
toeknee3mo ago
Record would be null, that's why I suggest livewire ownerRecord which should be where it comes from
RuiAlmeida
RuiAlmeidaOP3mo ago
Property [$ownerRecord] not found on component: [menu-component] Do you know somewhere it shows how to passs data from the compoenent to the form of that Modal? ;
toeknee
toeknee3mo ago
Can you provide the whole menu component?
RuiAlmeida
RuiAlmeidaOP3mo ago
<?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');
}
}
<?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');
}
}
RuiAlmeida
RuiAlmeidaOP3mo ago
for a better context this is what i'm trying to do: I want to open a modal when clicking that pencil and send that record The createAction is workign fine btw
No description
toeknee
toeknee3mo ago
So you want to call $this->butteryMenu then instead of livewire/owner record?
Want results from more Discord servers?
Add your server