Issue with search on relation & attribute as select name.
So i got a bit complicated select question:
I want to make an select field which has a relation with the ram relation (hasOne Animal). Where i can search the field based on country_code/registration_number/name. The 'select_name' is an attribute set in the model as followed:
But with the given code i get the following error, which makes sense but i don't really got a clue on how to solve it.
20 Replies
You cannot search Attributes as they don't exist on DB level
Okay, you already figured that out, I think 😅
Yea 🙂
I think it accepts a closure so you could build your own query:
->searchable(fn ($query) => $query->where(...))
Gonna try that, but im curious if then still takes the attribute as name
But lemme try
Not 100% sure, searchable is the right method in combination with relationship
It says in the docs it's accepted :). But if there is a better way im all ears, fairly new to filament
I think you can also do:
->relationship('relation', 'title_column', fn ($query) => $query->where())
Gonna try that
Just curious how the search string is passed
->searchable(fn ($query, $search) => $query->where('name', 'like', "%{$search}%")),
doesn't have the $search added
While in the docs it says it should xd
Unable to resolve dependency [Parameter #0 [ <required> $query ]] in class App\Filament\Resources\MatingResource
-,-
This is correct
However if it still doesn't work out, you can take full control of the search by using the
->getSearchResultsUsing()
Yea that does work with searching, but will not show the Attribute name
The select will fall back the the name
Im now doubting to even use a virtual column in the DB
maybe try getLabelFromRecordUsing()?
or whatever the method is
getOptionLabelUsing?
its the fromRecord version
getOptionLabelFromRecordUsing()
If i search the docs for this, https://filamentphp.com/docs/2.x/forms/fields
It also says its better to use a virtual column
The more i think about it, thats maybe the way to go.
Cause i tried almost every function i could find in the Select Class
HOORAY
This works
But still doubting if a virtual column would be better, since im going to need this "select_name" alot
Definitely, it's soo much easier with a virtual column