F
Filamentβ€’14mo ago
Jack Sleight

Passing an ID to a custom edit action

I'm trying to create an edit action on a custom page that allows me to edit a model directly from the page (rather than going to the normal resource page). It mostly works, but I'm running into problems passing the right model ID to the action as an argument. If I hard-code an ID it works perfectly.
67 Replies
Jack Sleight
Jack SleightOPβ€’14mo ago
This is what I have:
use Filament\Actions\EditAction;

public function editAction(): EditAction
{
return EditAction::make()
->record(fn (array $arguments) => Client::find($arguments['client']))
->form(ClientResource::formSchema());
}
use Filament\Actions\EditAction;

public function editAction(): EditAction
{
return EditAction::make()
->record(fn (array $arguments) => Client::find($arguments['client']))
->form(ClientResource::formSchema());
}
{{ ($this->editAction)(['client' => $client->id]) }}
{{ ($this->editAction)(['client' => $client->id]) }}
But I get Undefined array key "client" Side note formSchema is a method I added as I want to use the same form schema as the resource pages use. Weird thing is if I dd($arguments) the client key is there.
bwurtz999
bwurtz999β€’14mo ago
public function editAction(): EditAction
{
return EditAction::make()
->record(fn (array $arguments) => Client::find($arguments['client']))
->form(ClientResource::formSchema())
->extraAttributes(['id' => 'customId']);
}
public function editAction(): EditAction
{
return EditAction::make()
->record(fn (array $arguments) => Client::find($arguments['client']))
->form(ClientResource::formSchema())
->extraAttributes(['id' => 'customId']);
}
bwurtz999
bwurtz999β€’14mo ago
Oh wait sorry you wanted to pass a model
Jack Sleight
Jack SleightOPβ€’14mo ago
Yeah, well a model or an ID, I don't really mind, whatever works πŸ™‚
bwurtz999
bwurtz999β€’14mo ago
OK then yeah that should work What are you trying to accomplish exactly?
Jack Sleight
Jack SleightOPβ€’14mo ago
I have a custom page that displays some records, I want to be able to edit those records directly from that page (via edit modals), rather than having to go into the normal resource pages.
bwurtz999
bwurtz999β€’14mo ago
Do you know about simple resources?
Jack Sleight
Jack SleightOPβ€’14mo ago
Yeah they're all simple resources already. This page combines data from a bunch of different models (all of which need to be editable from this page). So just customising one of the default list pages isn't an option. If I do ->record(fn (array $arguments) => Client::find(1)) it works. The issue just seems to be something with the $arguments array.
bwurtz999
bwurtz999β€’14mo ago
Ok I see Where do you dd arguments? you might need to put that in use ($arguments) I don't think that's a standard variable that Filament will autofill
Jack Sleight
Jack SleightOPβ€’14mo ago
If I do ->record(fn (array $arguments) => dd($arguments['client'])) it outputs the ID, no error. It's really weird, it's like it gets called a second time with the key missing or something.
bwurtz999
bwurtz999β€’14mo ago
hmmm have you tried a full function () as opposed to an abbreviated one? Is $arguments defined by Filament? Or is that something you declare?
Jack Sleight
Jack SleightOPβ€’14mo ago
OK, yep that's exactly what's happening. The closure is actually being called five times, on the fifth call $arguments is blank.
Jack Sleight
Jack SleightOPβ€’14mo ago
No description
Jack Sleight
Jack SleightOPβ€’14mo ago
Yeah full function () is the same.
bwurtz999
bwurtz999β€’14mo ago
Oh wait... is client a full model instance or the record ID? You use Client::find($arguments['client'] and ['client' => $client->id]
Jack Sleight
Jack SleightOPβ€’14mo ago
I'm just passing in the ID currently. {{ ($this->editAction)(['client' => $client->id]) }}
bwurtz999
bwurtz999β€’14mo ago
Yeah so I think this might explain it from the docs
EditAction::make()
->record($this->post)
EditAction::make()
->record($this->post)
It looks like the EditAction wants the full model as the record try this {{ ($this->editAction)(['client' => $client]) }}
Jack Sleight
Jack SleightOPβ€’14mo ago
If I do that the model gets json encoded into the button HTML and $arguments['client'] just ends up as an array. The docs here https://filamentphp.com/docs/3.x/actions/adding-an-action-to-a-livewire-component#passing-action-arguments seem to suggest you have to pass in the ID?
bwurtz999
bwurtz999β€’14mo ago
I don't think you have to pass an ID
Jack Sleight
Jack SleightOPβ€’14mo ago
Ah OK
bwurtz999
bwurtz999β€’14mo ago
I think switching to an anonymous action would solve this for you using the EditAction might be the problem try switching to use Filament\Actions\Action;
Jack Sleight
Jack SleightOPβ€’14mo ago
Guess I then have to write custom code to update the record etc.?
bwurtz999
bwurtz999β€’14mo ago
return Action::make()
->mountUsing()
->form(ClientResource::formSchema())
->action(function ($data) {
// whatever you need to do
})
return Action::make()
->mountUsing()
->form(ClientResource::formSchema())
->action(function ($data) {
// whatever you need to do
})
yeah I think so You can use mountUsing() to set the form data I'm reading the docs again and you shouldn't have to do this... EditAction should work I don't know why it's not... sorry
Jack Sleight
Jack SleightOPβ€’14mo ago
Yeah it seems to be buggy. Feel like $arguments should just be populated all the time.
bwurtz999
bwurtz999β€’14mo ago
Wait... do you have client declared as a public variable for the page? public $client;
Jack Sleight
Jack SleightOPβ€’14mo ago
I don't.
bwurtz999
bwurtz999β€’14mo ago
->record($this->post) that's how it's used in the docs
Jack Sleight
Jack SleightOPβ€’14mo ago
Doesn't seem to help. But in that example how does $this->post get set? This page is displaying a list of clients, with an action for each one. So I assumed I needed to pass it in as an argument.
bwurtz999
bwurtz999β€’14mo ago
Are you rendering a custom Filament table to show all these different records?
Jack Sleight
Jack SleightOPβ€’14mo ago
Nope This is my (minimal) template currently
@foreach (App\Models\Client::all() as $client)
{{ $client->name }}
{{ ($this->editAction)(['client' => $client->id]) }}
@endforeach
@foreach (App\Models\Client::all() as $client)
{{ $client->name }}
{{ ($this->editAction)(['client' => $client->id]) }}
@endforeach
This is a completely custom view, it wont be a table.
bwurtz999
bwurtz999β€’14mo ago
OK yeah this all looks right can you dd ->mutateFormDataUsing
Jack Sleight
Jack SleightOPβ€’14mo ago
Sorry, whereabouts?
bwurtz999
bwurtz999β€’14mo ago
return EditAction::make() ->record(fn (array $arguments) => Client::find($arguments['client'])) ->mutateFormDataUsing(function ($data) {dd($data);}) ->form(ClientResource::formSchema());
Jack Sleight
Jack SleightOPβ€’14mo ago
Looks like the error is thrown before the mutateFormDataUsing callback get's called.
bwurtz999
bwurtz999β€’14mo ago
try adding return $data; sorry it has to return an array after the dd
Jack Sleight
Jack SleightOPβ€’14mo ago
Wont dd exit the request?
bwurtz999
bwurtz999β€’14mo ago
yes and it it will show us what data is actually getting submitted Oh wait sorry you're not having trouble with saving, you're having trouble filling the form correctly My bad
Jack Sleight
Jack SleightOPβ€’14mo ago
The error is still thrown before anything gets dumped
bwurtz999
bwurtz999β€’14mo ago
Yeah sorry I got confused
Jack Sleight
Jack SleightOPβ€’14mo ago
Yeah this is just opening the modal
bwurtz999
bwurtz999β€’14mo ago
right ->record(fn (array $arguments) => Client::find($arguments['client'])) this is the issue
Jack Sleight
Jack SleightOPβ€’14mo ago
Yeah
bwurtz999
bwurtz999β€’14mo ago
Can you share the output when you dd arguments? I know you said it's there but seeing it might help
Jack Sleight
Jack SleightOPβ€’14mo ago
So the callback is being called five times. dd gives me the right thing. If use dump the first four calls are all OK, only on the fifth is arguments blank. Weirdly if I dump the exception doesn't render, but it's still being thrown because it shows up in Ray. Output from the browser:
Jack Sleight
Jack SleightOPβ€’14mo ago
No description
Jack Sleight
Jack SleightOPβ€’14mo ago
Output from ray:
Jack Sleight
Jack SleightOPβ€’14mo ago
No description
Jack Sleight
Jack SleightOPβ€’14mo ago
And then:
Jack Sleight
Jack SleightOPβ€’14mo ago
No description
bwurtz999
bwurtz999β€’14mo ago
OK I'm starting to think the for loop is the problem It should only be called once Can you inspect the buttons and see if they all have the same ID? Or something that would make all of them trigger at once?
Jack Sleight
Jack SleightOPβ€’14mo ago
FWIW there are 13 clients, not 5
bwurtz999
bwurtz999β€’14mo ago
😭
Jack Sleight
Jack SleightOPβ€’14mo ago
Let me try just one... Nah same πŸ˜‚
bwurtz999
bwurtz999β€’14mo ago
Do you include <x-filament-actions::modals /> in your view? You also need <x-filament-actions::modals /> which injects the HTML required to render action modals. This only needs to be included within the Livewire component once, regardless of how many actions you have for that component.
Jack Sleight
Jack SleightOPβ€’14mo ago
I don't, but the modals work if I replace $arguments['client'] with an ID of a client.
bwurtz999
bwurtz999β€’14mo ago
OK try including it
Jack Sleight
Jack SleightOPβ€’14mo ago
OK I added that but no luck.
bwurtz999
bwurtz999β€’14mo ago
dang
Jack Sleight
Jack SleightOPβ€’14mo ago
I'm gonna have to run in a sec. Thanks so much for trying! Seems like it might be a bug?
bwurtz999
bwurtz999β€’14mo ago
yeah sorry I couldn't be more help I honestly don't know But I'm invested now haha I'm probably going to need this in the future
Jack Sleight
Jack SleightOPβ€’14mo ago
Ha, well if you find anything else I'd love to know! πŸ™‚ I'll submit a bug report if I'm still stuck next week. Seems like it should work, doesn't really make sense that $arguments is suddenly empty. Thanks again.
ShortlyPortly1964
ShortlyPortly1964β€’14mo ago
Hi @Jack Sleight and @bwurtz999 did you get any further with this issue as I have exactly the same problem. cheers
Jack Sleight
Jack SleightOPβ€’14mo ago
Haven’t got any further but I’ve submitted an issue: https://github.com/filamentphp/filament/issues/9234
GitHub
EditAction record() closure does not receive arguments reliably Β· I...
Package filament/filament Package Version 3.0.82 Laravel Version 10.28.0 Livewire Version 3.0.10 PHP Version 8.2.11 Problem description I'm using the Filament\Actions\EditAction on a custom pan...
ShortlyPortly1964
ShortlyPortly1964β€’13mo ago
Thanks Jack - I'll keep an eye on the issue - hopefully it will be solved soon. cheers
zoomZoom
zoomZoomβ€’13mo ago
Having the same issue here, i hope it gets fixed soon.
Want results from more Discord servers?
Add your server