Select with Relationship always Null
Hi,
I always get a null response when submitting a form using select that is related to another model and has multiple types. Even though I have cast the model to an array and have also created a controller for it.
Is this a bug or is there an error in my code?
11 Replies
Can anyone help me pleaseš
Not entirely sure what you are trying to do, but relationships shouldnāt be cast as arrays.
Iām making a regular form, but there is select (multiple) field with relationships. If select has multiple value, then its gonna store as an array on db right?
Nope if itās a multiple item relationship. Then the idās are not actually stored on the model, their parent id is stored on the relationships model. Unless it is a many to many relationship, in which case it needs a pivot table.
I would recommend spending some time looking at the relationship documentation on the laravel docs to get a more solid understanding.
Either way, there shouldnāt be a cast for any relationship in the app.
So the null response is generated because I used cast?
Thereās no way to know for sure. But based on what youāve shared you are not using relationships appropriately.
At least, thatās my opinion.
not sure if this related but there is a note about the foreign key column:
https://filamentphp.com/docs/3.x/forms/advanced#saving-data-to-a-belongsto-relationship
Looks like it's done because I forgot to make the parent column nullable, thank you guys
Just realized that the filled column is the column in the pivot table, and the main table column contains null. Is that how it works or is there an error?
The
->relationship()
always saves directly to the DB that's why the record is null for a moment
If it's a belongsToMany
data is always stored on the pivotI see, thanks Dennis. So how to display artist name based on artist_id to the table column? I'm kinda confused about this relationship things
My relationship looks like this
Trying this but the data does not appear
You probably want
primaryArtists.name
or similar then.