F
Filament3mo ago
Sydd

filter record in relations model

Hello everyone, I want some advice, direction... I have a Truck Driver model that is related to the db table drives... on the index page where all the truck drivers are, I show the number of kilometers per year, month, etc. how to correctly set such a filter ... so that I only have distinct values ​​from the db tables drives ... and then the query is also correctly adjusted in the driver according to the selected year. As stated below ... the filter returns the $id of records in drives with years, so I have 256x 2023 ... etc. If someone would direct me in which direction to go for the solution. Well thank you.
SelectFilter::make('year')
->label('Year')
->native(false)
->relationship('drives', 'year')
->options(Drive::query()->select('year')->distinct()->get()->pluck('year', 'year')->toArray())
SelectFilter::make('year')
->label('Year')
->native(false)
->relationship('drives', 'year')
->options(Drive::query()->select('year')->distinct()->get()->pluck('year', 'year')->toArray())
Seems to ->options dont aply to filer ....
Solution:
Thanks, for answer, its works according your advice. But finaly in the main table is calculated values, and dont work with sorting, search etc. I take care about create a db table as overview and next use. Rly thanks for yours engagement.
Jump to solution
3 Replies
awcodes
awcodes3mo ago
->relationship() accepts a 3rd argument which is a function to modify the query. Use that instead of options.
Solution
Sydd
Sydd3mo ago
Thanks, for answer, its works according your advice. But finaly in the main table is calculated values, and dont work with sorting, search etc. I take care about create a db table as overview and next use. Rly thanks for yours engagement.