F
Filament4mo ago
Peppe

Issue on Select Field with a second level relation.

Hi i write a simple Select with second level relation, and when i go to submit he give me this error
Forms\Components\Select::make('media.tags')
->label('Tag')
->placeholder('Seleziona')
->relationship('media.tags', 'label')
->required(),
Forms\Components\Select::make('media.tags')
->label('Tag')
->placeholder('Seleziona')
->relationship('media.tags', 'label')
->required(),
No description
Solution:
->pivotData(fn($get) => ['media_id' => $get('media_id') ])
->pivotData(fn($get) => ['media_id' => $get('media_id') ])
...
Jump to solution
10 Replies
LeandroFerreira
LeandroFerreira4mo ago
what kind of relationship?
Peppe
PeppeOP4mo ago
many to many, sorry
LeandroFerreira
LeandroFerreira4mo ago
https://filamentphp.com/docs/3.x/forms/fields/select#integrating-with-an-eloquent-relationship
Select::make('mediatags')
->relationship('yourrelationshipname', 'yourtitleattribute')
->multiple()
Select::make('mediatags')
->relationship('yourrelationshipname', 'yourtitleattribute')
->multiple()
Peppe
PeppeOP4mo ago
i tried right now, but still not working...
Forms\Components\Select::make('mediatags')
->label('Tag')
->multiple()
->preload()
->placeholder('Seleziona')
->relationship('media.tags', 'label')
->required(),
Forms\Components\Select::make('mediatags')
->label('Tag')
->multiple()
->preload()
->placeholder('Seleziona')
->relationship('media.tags', 'label')
->required(),
I've seen the docs before asking 😁
toeknee
toeknee4mo ago
media.tags is going through and the media isn't hasn't been set... you need to set the media_id
->pivotData([
'media_id' => @media_id here,
])
->pivotData([
'media_id' => @media_id here,
])
but I think what you really want is to select the media then have tags within the media it's self? which means you should use two relationships. Provide the full code ideally.
Peppe
PeppeOP4mo ago
Forms\Components\Select::make('mediatags')
->label('Tag')
->multiple()
->preload()
->placeholder('Seleziona')
->relationship('media.tags', 'label')
->pivotData(['media_id' => fn(Get $get) => $get('media_id') ])
->required(),

Forms\Components\TextInput::make('media_id'),
Forms\Components\Select::make('mediatags')
->label('Tag')
->multiple()
->preload()
->placeholder('Seleziona')
->relationship('media.tags', 'label')
->pivotData(['media_id' => fn(Get $get) => $get('media_id') ])
->required(),

Forms\Components\TextInput::make('media_id'),
i think i can't use like this?
toeknee
toeknee4mo ago
Looks like it should work if you have a media_id in the form
Peppe
PeppeOP4mo ago
Nope, the text input media_id is filled
No description
Solution
toeknee
toeknee4mo ago
->pivotData(fn($get) => ['media_id' => $get('media_id') ])
->pivotData(fn($get) => ['media_id' => $get('media_id') ])
Peppe
PeppeOP4mo ago
it works THANKSSSSSSS

Did you find this page helpful?