Custom Action
What I am trying to do:
I am trying to create a custom action like the AssociateAction however I want to customize it on my own form.
What I did:
I tried following this
https://filamentphp.com/docs/3.x/actions/modals#modal-forms, specifically this part
I've changed the $record into $post and the author() into just
->user_id
and right now I am testing it ->associate(1)
by just passing an int to see if it works.
My issue/the error:
if I kept the $post-user_id()->associate(1)
i get the error BadMethodCall
Call to undefined method App\Models\Equipment::user_id()
changing it into post-user_id->associate(1)
i get the error Call to a member function associate() on null
Code:
Ignore the other headerActions like AssociateAction and 'Borrow'
Thank you !!!!11 Replies
Why dont you directly assign the value to it and then save the model?
make sure the Equipment model has
user_id
in the fillable arrayThanks it seem to should work fine now since I get a new error after trying the code you gave.
I get an error
SQLSTATE[HY000]: General error: 1364 Field 'name' doesn't have a default value
I also have in my
AppServiceProvider.php
yeah, it seems you dont have "name" in the fillable array of your model
I still get the same error SQLSTATE[HY000]: General error: 1364 Field 'name' doesn't have a default value
In my model I have this
ok, then this tells me that the 'name' column on the equipment table is not nullable. In that case, the model is expecting a value but isnt receiving one
it works but it's not actually associating like I wanted to but instead create a new record with user_id that was selected
$equipment->user()->associate(1)
You’re not using the relationship.
You might need to look up the user model first though.
I tried but it still does the same, I also added the
use App\Models\User;
Also for some reason the ->options(Equipment::query()->pluck('name','barcode'))
stopped working so I tried temporarily the options with hard coded options and I get this error Filament\Forms\Components\Select::isOptionDisabled(): Argument #2 ($label) must be of type string, null given
But focusing on the main issue is that it does the same where it also creates a new record instead of associating the user_id to the VOMSomething is off in your relationships then. Hard to say without seeing all the code. Sorry.
Here are the codes for the User model
I'll send the next Equipmentmodel
Equipment model code
bump, is the problem the type of relationship I used for associate not to work?
hi, i've posted all the codes, I really need help. Thank you!
I think the problem is that equipment is null in the callback of the action.