F
Filament11mo ago
Darpan

How to use nested relationship in SelectFilter?

I have State, City and Suburb models. Suburb belongs to City, City belongs to State. In table TextColumn::make('city.state.name') is working fine. But in table select filter I am getting following error. SelectFilter::make('state')->relationship('city.state', 'name') Call to undefined method App\Models\Suburb::city.state()
4 Replies
cheesegrits
cheesegrits11mo ago
You can't use dotted notation for relationship names. I'm assuming this filter is on the Suburbs table? In which case you could try creating a BelongsToThrough relationship from the Suburb model to State. Or create a custom filter, where you can use whatever query you want to build the Select options. https://github.com/staudenmeir/belongs-to-through
GitHub
GitHub - staudenmeir/belongs-to-through: Laravel Eloquent BelongsTo...
Laravel Eloquent BelongsToThrough relationships. Contribute to staudenmeir/belongs-to-through development by creating an account on GitHub.
Darpan
Darpan11mo ago
Thanks for your reply, https://github.com/staudenmeir/belongs-to-through this works well, but it shows city name in filter indicator which can be fixed by using indicateUsing().
SelectFilter::make('state')
->relationship('state', 'name')
->indicateUsing(function ($data): ?string {
if(!$data['value']) {
return null;
}
return 'State: ' . State::find($data['value'])->name;
})
SelectFilter::make('state')
->relationship('state', 'name')
->indicateUsing(function ($data): ?string {
if(!$data['value']) {
return null;
}
return 'State: ' . State::find($data['value'])->name;
})
GitHub
GitHub - staudenmeir/belongs-to-through: Laravel Eloquent BelongsTo...
Laravel Eloquent BelongsToThrough relationships. Contribute to staudenmeir/belongs-to-through development by creating an account on GitHub.
cheesegrits
cheesegrits11mo ago
Out of interest, try using a dotted notation in the name field., like state.name (assuming you have a 'state' relationship on City). Don't know if it'll work, but worth trying.
Darpan
Darpan11mo ago
No, it does not work, getting following error: Filament\Tables\Filters\SelectFilter::getRelationship(): Return value must be of type Illuminate\Database\Eloquent\Relations\Relation|Illuminate\Database\Eloquent\Builder, null returned