#How to pass data to an EditAction

9 messages · Page 1 of 1 (latest)

quiet oak
#

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');
                });
    }

Do i need anything in the mount() ?
(I have a similar createAction in the compoenent that's working fine
)

kindred vigilBOT
#

To help others find answers, you can mark your question as solved via Right click solution message -> Apps -> ✅ Mark Solution

versed glacier
#
 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?

quiet oak
#

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

versed glacier
#

Record would be null, that's why I suggest livewire ownerRecord which should be where it comes from

quiet oak
#

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? ;

versed glacier
#

Can you provide the whole menu component?

quiet oak
#
<?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');
    }
}

#

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