F
Filament14mo ago
Chriis

How to manage n-n relationship in this case ?

Hello, I'm having a bit of trouble figuring out how to handle the data registration in the intermediate table in this relationship. I save the framework ID in the 'sheets' table, but once the framework is determined, another select appears and allows choosing skills from this selected framework in the previous select. I was thinking of attaching each skill in the afterCreate() function, but when the resource is edited, it becomes more complicated ... Part of the Form with selects :
Select::make('framework_id')
->label('Référentiel')
->relationship('sheetFramework', 'title')
->native(false)
->live()
->afterStateUpdated(fn (Forms\Set $set) => $set('framework_competencies', null)),
Select::make('framework_competencies')
->label('Compétences')
->options(function (Builder $query, Forms\Get $get) {
return Competency::where('competency_framework_id', $get('framework_id'))->pluck('title', 'id')->toArray();
})
->multiple()
->live()
->hidden(fn (Forms\Get $get) => blank($get('framework_id')))
Select::make('framework_id')
->label('Référentiel')
->relationship('sheetFramework', 'title')
->native(false)
->live()
->afterStateUpdated(fn (Forms\Set $set) => $set('framework_competencies', null)),
Select::make('framework_competencies')
->label('Compétences')
->options(function (Builder $query, Forms\Get $get) {
return Competency::where('competency_framework_id', $get('framework_id'))->pluck('title', 'id')->toArray();
})
->multiple()
->live()
->hidden(fn (Forms\Get $get) => blank($get('framework_id')))
Help would be really appreciated 🙂
No description
No description
31 Replies
toeknee
toeknee14mo ago
Wouldn't framework_competencies be a relationship and as such just saves directly to the relationship?
Chriis
ChriisOP14mo ago
It could be if a sheet would have every competencies of a competency_framework, however, a sheet can have some but not all competencies of a competency_framework This is why its a little bit tricky
toeknee
toeknee14mo ago
Can you refind the options with a custom option list based on selections relative to the sheet?
Chriis
ChriisOP14mo ago
I'm sorry, but I don't understand what you're saying 😬
toeknee
toeknee14mo ago
So I need a better understanding I suppose of what you are trying to do, i.e. with conditions etc
Chriis
ChriisOP14mo ago
Ok, so I'm trying to save the competencies of a sheet into the pivot table competency_sheet but because I dont use ->relationship() for the input where I select competencies I want ... nothing is currently saved in the DB The select in question
Select::make('framework_competencies')
->label('Compétences')
->options(function (Builder $query, Forms\Get $get) {
return Competency::where('competency_framework_id', $get('framework_id'))->pluck('title', 'id')->toArray();
})
->multiple()
->live()
->hidden(fn (Forms\Get $get) => blank($get('framework_id')))
Select::make('framework_competencies')
->label('Compétences')
->options(function (Builder $query, Forms\Get $get) {
return Competency::where('competency_framework_id', $get('framework_id'))->pluck('title', 'id')->toArray();
})
->multiple()
->live()
->hidden(fn (Forms\Get $get) => blank($get('framework_id')))
The best scenario would be to use ->relationship() BUT having only the competencies from the competency framework selected above
toeknee
toeknee14mo ago
But that should work? I suspect your issue is you need to set relationship in the above, but only return the options if you have a framework_id so
->options(function (Builder $query, Forms\Get $get) {
return $get('framework_id') > 0 ? Competency::where('competency_framework_id', $get('framework_id'))->pluck('title', 'id')->toArray() : [];
})
->options(function (Builder $query, Forms\Get $get) {
return $get('framework_id') > 0 ? Competency::where('competency_framework_id', $get('framework_id'))->pluck('title', 'id')->toArray() : [];
})
cleaner again
->options(function (Builder $query, Forms\Get $get) {
$fid = (int) $get('framework_id');
return $fid > 0 ? Competency::where('competency_framework_id', $fid)->pluck('title', 'id')->toArray() : [];
})
->options(function (Builder $query, Forms\Get $get) {
$fid = (int) $get('framework_id');
return $fid > 0 ? Competency::where('competency_framework_id', $fid)->pluck('title', 'id')->toArray() : [];
})
Chriis
ChriisOP14mo ago
No but my options are working fine
toeknee
toeknee14mo ago
But you said the options were returning all?
Chriis
ChriisOP14mo ago
Everything is working perfectly, its just that competencies selected are not saved in the DB
toeknee
toeknee14mo ago
So to save them to the DB you pass them into by using the ->relationship()
Chriis
ChriisOP14mo ago
Ah, sorry if you understood taht I put ->relationship() on the select on top of the ->options() ?
toeknee
toeknee14mo ago
I didn't see that in this. ? But yes you would tend to do that. Are you syaing if you define it's a relationship it's still not saving them as a relationship? and your model for the relationship has sheetFramework as the relationship class? Try updating the select name to be sheetFramework too if that is the relationship name on the model.
Chriis
ChriisOP14mo ago
Ok so I added relationship and it worked ... My bad honestly I didn't thought it was possible to have ->relationship() and ->options on a select because for me ->relationship() is already creating the options But I have another problem Idk if its due to ->relationship() Now the competencies options are not refreshed when I change the framework
Chriis
ChriisOP14mo ago
Chriis
ChriisOP14mo ago
Framework 2 have C2.1 .... C2.2 ... etc
toeknee
toeknee14mo ago
Is the framework coded to be ->live() ?
Chriis
ChriisOP14mo ago
Oh ok I'm dumb ahaha Yes its live but I didnt renamed the afterStateUpdate and I renamed the input ...
toeknee
toeknee14mo ago
😉
Chriis
ChriisOP14mo ago
Ah, nope so now it clear the competencies selected But the options are still the one of the first framework selected, they are not refreshed
toeknee
toeknee14mo ago
They should be refreshed, but as a hacky solution, afterStateUpdated() you can load the records and uset the setter to set that relationships' options based on the selection, but only once it is shown. It's important to note, it can only be updated if it's shown.
Chriis
ChriisOP14mo ago
Chriis
ChriisOP14mo ago
Ok so because its hidden it didnt refresh the query, but I dont understand when you say "you can load the records" I tried without hidding the input and it works well but revealing the input when a framework is selected is better I don't understand how I can use afterStateUpdated() for changing the options query
toeknee
toeknee14mo ago
You can first try it with disabled opposed to hidden.
Chriis
ChriisOP14mo ago
Yes, so it looks like when a select is disabled you can still write in it 😮
toeknee
toeknee14mo ago
It should be non-clickable
Chriis
ChriisOP14mo ago
Chriis
ChriisOP14mo ago
Yes its weird
toeknee
toeknee14mo ago
Ahh you have searchable, so you can search but not select. Disable searchable too when you want it disabled
Chriis
ChriisOP14mo ago
Ah ok thanks a lot, I will do that 🙂 I just looked and I dont have searchable, its the multiple(). And know I no longer can search in it when disabled (I have changed nothing 😅 ) but I have another bug ... I can't open the select and it don't have placeholder 😬
Chriis
ChriisOP14mo ago

Did you find this page helpful?