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
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.
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()
.
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.
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.
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