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
That is weird. It gets saved correctly? What kind of relationship is it?
Yes their are correctly saved, it's a belongToMany relationship
use ->preload(), this will also show some options on the create page, consider using optionsLimit(10) or so if your select is searchable
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');
}
how about removing
from the relationship and put them in modifyQueryUsing ?
No changes
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
The problem occur only when i have Pivot field and using it in ->wherePivot
Yes values in table are correct