Error in Action: "$ownerRecord must not be accessed before initialization"
Hello,
I'm trying to access livewire->ownerRecord inside an action but i'm getting the error in the title.
Here is my code
20 Replies
it worked all of a sudden but $quote->id (or any attribute) is now null
dd($quote) I suspect the relationship quote is broken
@toeknee dd($quote) gives me the model class
In case it's relevant the action is inside a relation manager
I'm wondering if the reason could be related to the fact that the relation is a HasManyThrough
Try using $record instead of $quote as the param name in your action() closure. Parameter injection is done by name, so Filament knows what $record is, it doesn't know what $quote is.
So when the docs give examples of callback closures with injected param names, you typically have to use those exact names.
@Hugh Messenger Thanks a lot. it worked. I had no idea that was necessary.
When you think about it, it makes sense. Filament doesn't know anything about your app or usage, it has no idea what a $quote is, or how to inject it into the closure.
I thought it would know that using the positioning of the arguments
Again, when you think about it, there is no argument positioning, as closures are anonymous functions with no signature.
This is one of those slightly tricky things in Filament, which there isn't really any IDE magic or language features that can help. To figure out what param are available in any given callback closure, you pretty much have to dig thru the docs. Although you get used to it after a while, and pretty much get to know what's going to be available where.
Yeah I think I need to read a bit more about closure to fully grasp it
There's nothing really magic about closures, they are just little anonymous, inline functions that can get called. The magic part is how the paramater injection works, which is always going to depend on the context. If it's a closure on a form field, then there is most likely a $state, $get / $set, etc. In a table row action there will be a $record. Etc.
and you can insert those in any order you want right ? and each of them is optional?
Correct. Filament just looks at all the parameter names (and types) you specified in your closure, and attempts to match then against whatever data it has in the context of the callback.
In your first attempt, notice that you got a model class for $quote. That's where Filament went "OK, I don't know what $quote is, but the Quote type is a model class, so I'll give him an instance of that".
When you changed the name to $record, Filament knows you want the table row record, because the context is a table row action, and $record is one of the predefined param names it expects in that context.
That's pretty neat!
Thanks for the explanation.
N/p
@Hugh Messenger one last question. Any idea where I can find all the possible parameters I can pass to the closure ?
I have a follow up question here https://discord.com/channels/883083792112300104/1152920132897755236
How do i use ownerRecord and record inside a successNotification for that action
There isn't really a single place in the docs, because it's so dependent on where your closure is. So you just have to dig in to the docs for whatever it is you are using a closure on. Each section of the docs (tables, forms, actions, etc) have a generic "Utility Injection" section (search for "inject"), but they don't always list everything. You have to read the code exaqmples ffor whatever it is you are doing.
Noted! Thanks @Hugh Messenger
I'll respond on that thread.