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
This is what I have:
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.Oh wait sorry you wanted to pass a model
Yeah, well a model or an ID, I don't really mind, whatever works π
OK then yeah that should work
What are you trying to accomplish exactly?
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.
Do you know about
simple
resources?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.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 autofillIf 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.hmmm
have you tried a full
function ()
as opposed to an abbreviated one?
Is $arguments
defined by Filament? Or is that something you declare?OK, yep that's exactly what's happening. The closure is actually being called five times, on the fifth call
$arguments
is blank.Yeah full
function ()
is the same.Oh wait... is
client
a full model instance or the record ID?
You use Client::find($arguments['client']
and
['client' => $client->id]
I'm just passing in the ID currently.
{{ ($this->editAction)(['client' => $client->id]) }}
Yeah so I think this might explain it
from the docs
It looks like the EditAction wants the full model as the record
try this
{{ ($this->editAction)(['client' => $client]) }}
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?I don't think you have to pass an ID
Ah OK
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;
Guess I then have to write custom code to update the record etc.?
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... sorryYeah it seems to be buggy. Feel like
$arguments
should just be populated all the time.Wait... do you have
client
declared as a public variable for the page?
public $client;
I don't.
->record($this->post)
that's how it's used in the docsDoesn'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.Are you rendering a custom Filament table to show all these different records?
Nope
This is my (minimal) template currently
This is a completely custom view, it wont be a table.
OK
yeah this all looks right
can you
dd
->mutateFormDataUsingSorry, whereabouts?
return EditAction::make()
->record(fn (array $arguments) => Client::find($arguments['client']))
->mutateFormDataUsing(function ($data) {dd($data);})
->form(ClientResource::formSchema());
Looks like the error is thrown before the
mutateFormDataUsing
callback get's called.try adding
return $data;
sorry
it has to return an array
after the ddWont
dd
exit the request?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
The error is still thrown before anything gets dumped
Yeah sorry I got confused
Yeah this is just opening the modal
right
->record(fn (array $arguments) => Client::find($arguments['client']))
this is the issueYeah
Can you share the output when you
dd
arguments?
I know you said it's there but seeing it might helpSo 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:Output from ray:
And then:
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?
FWIW there are 13 clients, not 5
π
Let me try just one...
Nah same π
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.
I don't, but the modals work if I replace
$arguments['client']
with an ID of a client.OK try including it
OK I added that but no luck.
dang
I'm gonna have to run in a sec.
Thanks so much for trying!
Seems like it might be a bug?
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
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.Hi @Jack Sleight and @bwurtz999 did you get any further with this issue as I have exactly the same problem. cheers
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...
Thanks Jack - I'll keep an eye on the issue - hopefully it will be solved soon. cheers
Having the same issue here, i hope it gets fixed soon.