F
Filamentā€¢5mo 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ā€¢5mo ago
Can anyone help me pleasešŸ˜ž
awcodes
awcodesā€¢5mo ago
Not entirely sure what you are trying to do, but relationships shouldnā€™t be cast as arrays.
kisut
kisutOPā€¢5mo 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ā€¢5mo 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ā€¢5mo ago
So the null response is generated because I used cast?
awcodes
awcodesā€¢5mo 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ā€¢5mo 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ā€¢5mo 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ā€¢5mo 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ā€¢5mo 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ā€¢5mo ago
You probably want primaryArtists.name or similar then.
Want results from more Discord servers?
Add your server