F
Filament7mo 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
toeknee7mo ago
Wouldn't framework_competencies be a relationship and as such just saves directly to the relationship?
Chriis
Chriis7mo 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
toeknee7mo ago
Can you refind the options with a custom option list based on selections relative to the sheet?
Chriis
Chriis7mo ago
I'm sorry, but I don't understand what you're saying 😬
toeknee
toeknee7mo ago
So I need a better understanding I suppose of what you are trying to do, i.e. with conditions etc
Chriis
Chriis7mo 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
toeknee7mo 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
Chriis7mo ago
No but my options are working fine
toeknee
toeknee7mo ago
But you said the options were returning all?
Chriis
Chriis7mo ago
Everything is working perfectly, its just that competencies selected are not saved in the DB
toeknee
toeknee7mo ago
So to save them to the DB you pass them into by using the ->relationship()
Chriis
Chriis7mo ago
Ah, sorry if you understood taht I put ->relationship() on the select on top of the ->options() ?
toeknee
toeknee7mo 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
Chriis7mo 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
Chriis7mo ago
Chriis
Chriis7mo ago
Framework 2 have C2.1 .... C2.2 ... etc
toeknee
toeknee7mo ago
Is the framework coded to be ->live() ?
Chriis
Chriis7mo ago
Oh ok I'm dumb ahaha Yes its live but I didnt renamed the afterStateUpdate and I renamed the input ...
toeknee
toeknee7mo ago
😉
Chriis
Chriis7mo 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
toeknee7mo 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
Chriis7mo ago
Chriis
Chriis7mo 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
toeknee7mo ago
You can first try it with disabled opposed to hidden.
Chriis
Chriis7mo ago
Yes, so it looks like when a select is disabled you can still write in it 😮
toeknee
toeknee7mo ago
It should be non-clickable
Chriis
Chriis7mo ago
Chriis
Chriis7mo ago
Yes its weird
toeknee
toeknee7mo ago
Ahh you have searchable, so you can search but not select. Disable searchable too when you want it disabled
Chriis
Chriis7mo 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
Chriis7mo ago