Dependant dropdowns don't load relationships on the Edit page.

Hello everyone. I have a resource called BriefResource and the model itself has a belongsTo() relationship to another model called ContentTypeVariation. The ContentTypeVariation has a belongsTo() relationship to ContentType model.
Now, on BriefResource.php i have this -
Forms\Components\Select::make('content_type_id')
->label('Content Type')
->options(ContentType::query()->pluck('name', 'id'))
->reactive()
->afterStateUpdated(fn (callable $set, $state) => $set('content_type_variation_id', null))
->default(function ($record) {
return $record ? optional($record->content_type_variation)->content_type_id : null;
}),

Forms\Components\Select::make('content_type_variation_id')
->relationship('content_type_variation', 'name')
->label('Content Type Variation')
->options(function (callable $get) {
$contentTypeId = $get('content_type_id');
return ContentTypeVariation::where('content_type_id', $contentTypeId)
->pluck('name', 'id');
})
->reactive()
->default(function ($record) {
return $record ? $record->content_type_variation_id : null;
})
Forms\Components\Select::make('content_type_id')
->label('Content Type')
->options(ContentType::query()->pluck('name', 'id'))
->reactive()
->afterStateUpdated(fn (callable $set, $state) => $set('content_type_variation_id', null))
->default(function ($record) {
return $record ? optional($record->content_type_variation)->content_type_id : null;
}),

Forms\Components\Select::make('content_type_variation_id')
->relationship('content_type_variation', 'name')
->label('Content Type Variation')
->options(function (callable $get) {
$contentTypeId = $get('content_type_id');
return ContentTypeVariation::where('content_type_id', $contentTypeId)
->pluck('name', 'id');
})
->reactive()
->default(function ($record) {
return $record ? $record->content_type_variation_id : null;
})
When im creating a new brief, i indeed get the correct content_type_variation_id in the DB.. but when im editing the entry, the dropdowns don't get populated at all.
3 Replies
prowler
prowlerOP8mo ago
Hmm, when loading explicitly the relationship on the EditBrief.php file like this -
protected function mutateFormDataBeforeFill(array $data): array
{
$contentTypeVariation = ContentTypeVariation::find($data['content_type_variation_id']);
$contentTypeId = $contentTypeVariation->content_type->id;
$data['content_type_id'] = $contentTypeId;

return $data;
}
protected function mutateFormDataBeforeFill(array $data): array
{
$contentTypeVariation = ContentTypeVariation::find($data['content_type_variation_id']);
$contentTypeId = $contentTypeVariation->content_type->id;
$data['content_type_id'] = $contentTypeId;

return $data;
}
it works just fine.. but why? is it because Brief doesn't have a direct relationship to ContentType but only via ContentTypeVariation? should i define some "belongsToThrough" relationship?
Povilas K
Povilas K8mo ago
Not sure what doesn't work in your case but I found an example from our FilamentExamples of a 3-level dependent dropdown with edit form, please compare to our code. Doesn't fit in code limit and on mobile screenshot so sending two screenshots 🙂
No description
No description
toeknee
toeknee8mo ago
Also this is bad, it causes duplicate queries looking through the returned array
->options(ContentType::query()->pluck('name', 'id'))
->options(ContentType::query()->pluck('name', 'id'))
use
->options(fn() => ContentType::query()->pluck('name', 'id'))
->options(fn() => ContentType::query()->pluck('name', 'id'))
As Povilas pointed out, you can use afterStateUpdated to push the array of data too but if the first and second is reactive it should fetch fine. ahhh the second one is a relationship I've got a field that's your problem
Want results from more Discord servers?
Add your server