Fill Not Working - MultiSelect - With Pivot Table Relationship

I'm trying to create an Action to select some Travelers to attach to an Trip. I have a table called 'trip_travelers' that has 'trip_id' and 'traveler_id'. Trip Model:
public function travelers(): BelongsToMany
{
return $this->belongsToMany(Traveler::class, 'trip_travelers');
}
public function travelers(): BelongsToMany
{
return $this->belongsToMany(Traveler::class, 'trip_travelers');
}
The same with Travelers. I made a relationManager with createAction as default and it works like a Charm, creating records and already attaching with Model. But I need to create an Action to Attach existing Travelers so I decided to use a MultiSelect with custom Action to open a modal and select the Travelers. The problem is that fillForm is not filling my form with selected options. My Action:
Action::make('Manage Traveler')
->record($this->ownerRecord)
->model(Traveler::class)
->fillForm(function ($record) {
return $this->ownerRecord->travelers()->get()->toArray();
})
->form(function () {
return [
MultiSelect::make('travelers')
->model(Trip::class)
->relationship('travelers', 'first_name')
->preload()
->options(
function () {
return Traveler::query()->where('client_id', $this->ownerRecord->client_id)->pluck('first_name', 'id');
}
)
->createOptionForm(function () {
return Traveler::getForm();
})
->required()
];
})
Action::make('Manage Traveler')
->record($this->ownerRecord)
->model(Traveler::class)
->fillForm(function ($record) {
return $this->ownerRecord->travelers()->get()->toArray();
})
->form(function () {
return [
MultiSelect::make('travelers')
->model(Trip::class)
->relationship('travelers', 'first_name')
->preload()
->options(
function () {
return Traveler::query()->where('client_id', $this->ownerRecord->client_id)->pluck('first_name', 'id');
}
)
->createOptionForm(function () {
return Traveler::getForm();
})
->required()
];
})
My relationManager is attached to TripResource.
2 Replies
leoblanski
leoblanskiOP10mo ago
If I debug ($this->ownerRecord->travelers()->get()->toArray()); it returns correctly the data, but for some reason is not filling the form.
leoblanski
leoblanskiOP10mo ago
No description
Want results from more Discord servers?
Add your server