SelectFilter by 2 level relationship
I want to make a SelectFilter by country in a table where I show vehicles.
The data model is as follows:
Vehicle
- id
- name
- place_id
Place
- id
- name
- country_id
Country
- id
- code
- name
That is, a Vehicle belongs to a Place and a Place has a Country.
My current code is as follows, the filter shows it but the changes are not applied:
Thanks
Solution:Jump to solution
https://v2.filamentphp.com/tricks/use-selectfilter-on-distant-relationships
Can you check this article...
Filament
Use SelectFilter on distant relationships by Hugh Messenger - Trick...
Filament is a collection of tools for rapidly building beautiful TALL stack apps, designed for humans.
15 Replies
I think your problem is with
SelectFilter::make('place.country.code')
the name of SelectFilter I don't think should be named like this can you try only for example country and show some results?I get a SQL error:
`
Did you have declare functions in your models for relationships?
Yes, I do:
Vehicle.php
Place.php
Country.php
Can you remove the .code just let it place.country
Because I think the code should came from the options
And this SelectFilter you are implement it in VehicleResource?
The result is the same, the filter is shown but doesn't do nothing 😦
The countries how did you save in the DB
And please try to remove the multiple for just once
The result without is the same
SelectFilter::make('code')
->label(__('Country'))
->relationship(place.country.code', 'name')
Can you try like this?
SelectFilter::make('place.country.code')
->label(__('Country'))
->options(fn () => Country::pluck('code', 'id')->toArray())
Or like thisThis one throws an error:
Filament\Support\Services\RelationshipJoiner::prepareQueryForNoConstraints(): Argument #1 ($relationship) must be of type Illuminate\Database\Eloquent\Relations\Relation, null given, called in /Users/rsv/Herd/my_project/vendor/filament/forms/src/Components/Select.php on line 763
This one doesn't work too 😦
Solution
https://v2.filamentphp.com/tricks/use-selectfilter-on-distant-relationships
Can you check this article
Filament
Use SelectFilter on distant relationships by Hugh Messenger - Trick...
Filament is a collection of tools for rapidly building beautiful TALL stack apps, designed for humans.
I think is usefull
Thank you very much for your help! It's working now!
This is the code: