F
Filament•9mo ago
kisut

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?
No description
No description
No description
11 Replies
kisut
kisutOP•9mo ago
Can anyone help me pleasešŸ˜ž
awcodes
awcodes•9mo ago
Not entirely sure what you are trying to do, but relationships shouldn’t be cast as arrays.
kisut
kisutOP•9mo ago
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?
awcodes
awcodes•9mo ago
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.
kisut
kisutOP•9mo ago
So the null response is generated because I used cast?
awcodes
awcodes•9mo ago
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.
Lara Zeus
Lara Zeus•9mo ago
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
kisut
kisutOP•9mo ago
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?
Dennis Koch
Dennis Koch•9mo ago
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 pivot
kisut
kisutOP•9mo ago
I 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
public function primaryArtists()
{
return $this->belongsToMany(Artist::class, 'primary_artist_track', 'track_id', 'artist_id');
}
public function primaryArtists()
{
return $this->belongsToMany(Artist::class, 'primary_artist_track', 'track_id', 'artist_id');
}
My relationship looks like this
Tables\Columns\TextColumn::make('primaryArtists.artist_id')
Tables\Columns\TextColumn::make('primaryArtists.artist_id')
Trying this but the data does not appear
Dennis Koch
Dennis Koch•9mo ago
You probably want primaryArtists.name or similar then.

Did you find this page helpful?