F
Filament14mo ago
qcol

Call to a member function getKey() on null

Hi, I am trying to use a custom class to create a record. According to the documentation, https://filamentphp.com/docs/3.x/actions/prebuilt-actions/create#customizing-the-creation-process
"$model is the class name of the model, but you can replace this with your own hard-coded class if you wish." That's why I'm using it like this:
->using(function ($livewire, array $data, Tables\Actions\CreateAction $action): Void { WhdocMovesClass::move($data, $livewire, $action); }) In my class, I make many calls and writes to the database. The operations execute correctly, however, I get an error at the end: Call to a member function getKey() on null I read somewhere that the function should return a Model, but in my case in ->using() many operations are executed on the database, so how to return one model?
Solution:
Your using() must return a model in CreateAction, that's just baked in. It's CreateAction. It exists to create a model. There is other internal housekeeping done after the process() call that runs your using(), which requires the created model. If that doesn't fit your needs, then you probably just need to use a custom Action, rather than modifying CreateAction....
Jump to solution
2 Replies
Solution
cheesegrits
cheesegrits14mo ago
Your using() must return a model in CreateAction, that's just baked in. It's CreateAction. It exists to create a model. There is other internal housekeeping done after the process() call that runs your using(), which requires the created model. If that doesn't fit your needs, then you probably just need to use a custom Action, rather than modifying CreateAction.
qcol
qcolOP14mo ago
I finally eliminated the error by returning $livewire->getRelationship()->getModel() at the end of the class; but you're right, I made it unnecessarily confusing, I think I'll create a custom action.
Want results from more Discord servers?
Add your server