Multi-select in relationship doesn't display values on edit

It's probably a beginner's question, and I apologize for that, but when I use a multi-select type linked to a relationship in Eloquent, I can save the values successfully. However, when I go into editing mode, the field is empty. Thank you in advance for your assistance. Forms\Components\Select::make('contexts') ->label(__('smartxsp::campaign.edit-contexts')) ->multiple() ->relationship(name: 'contexts', titleAttribute: 'name') ->columnSpan("full"),
8 Replies
josef
josef16mo ago
That is weird. It gets saved correctly? What kind of relationship is it?
olivier_oursblanc
olivier_oursblancOP16mo ago
Yes their are correctly saved, it's a belongToMany relationship
Shinra
Shinra16mo ago
use ->preload(), this will also show some options on the create page, consider using optionsLimit(10) or so if your select is searchable
olivier_oursblanc
olivier_oursblancOP16mo ago
I try but it's not working better The code in the ressource : Forms\Components\Select::make('packages') ->label(__('smartxsp::campaign.edit-packages')) ->helperText(__('smartxsp::campaign.edit-packages-help')) ->multiple() ->relationship( name: 'packages', titleAttribute: 'name', modifyQueryUsing: fn (Builder $query) => Package::allowed(), ) ->getOptionLabelFromRecordUsing(fn (Package $record) => "{$record->name}") ->searchable(['name']) ->forceSearchCaseInsensitive() ->columnSpan("full") ->preload() , And in Eloquent model : public function packages() { return $this->belongsToMany(\SmartXSP\Brain\Models\Package::class)->wherePivot('exclude', 0)->withPivot('exclude'); }
Shinra
Shinra16mo ago
how about removing
->wherePivot('exclude', 0)->withPivot('exclude')
->wherePivot('exclude', 0)->withPivot('exclude')
from the relationship and put them in modifyQueryUsing ?
olivier_oursblanc
olivier_oursblancOP16mo ago
No changes
Shinra
Shinra16mo ago
How about making sure the values saved in the table (from the relationship's save ) is a correct value ? I always prefer assigning local_id and foreign id in the relationships
olivier_oursblanc
olivier_oursblancOP16mo ago
The problem occur only when i have Pivot field and using it in ->wherePivot Yes values in table are correct
Want results from more Discord servers?
Add your server