F
Filament15mo ago
Jaw

How can i search a related model name in TextColumn?

TextColumn::make('post.name')
->sortable()
->searchable(),
TextColumn::make('post.name')
->sortable()
->searchable(),
i cannot search for a post name.
Solution:
Something like this ```php TextColumn::make('user.name') ->label(__('By')) ->searchable(...
Jump to solution
8 Replies
Solution
Patrick Boivin
Patrick Boivin15mo ago
Something like this
TextColumn::make('user.name')
->label(__('By'))
->searchable(
query: fn(Builder $query, string $search) => $query->whereHas(
relation: 'user',
callback: fn(Builder $q) => $q->where('name', 'like', "%{$search}%")
)
),
TextColumn::make('user.name')
->label(__('By'))
->searchable(
query: fn(Builder $query, string $search) => $query->whereHas(
relation: 'user',
callback: fn(Builder $q) => $q->where('name', 'like', "%{$search}%")
)
),
cheesegrits
cheesegrits15mo ago
I don't think you need to override the query? You could could just do ...
TextColumn::make('post.name')
->sortable()
->searchable(['posts.name']),
TextColumn::make('post.name')
->sortable()
->searchable(['posts.name']),
NOTE that it's posts.name in the searchable(), ie. the table name, not the relation name.
Patrick Boivin
Patrick Boivin15mo ago
Very nice! Much simpler, but a bit unintuitive in the context of the form builder.
bwurtz999
bwurtz99915mo ago
My question isn't directly related, but this is the closest relevant question I could find here. I use a custom union of two tables in my relation manager getTableQuery() to return the proper set of data. When I try to search, all the results from the set that is union-ed to the first set is shown. I have used Telescope to dig into the queries, and the filtering is only being done to the first part of the query When I realized this, I tried using a custom query
->searchable(
query: function(Builder $query, string $search, RelationManager $livewire) {
$class = new TeamProfileItemsRelationManager;
return $class->getDescriptionSearchQuery($search, $livewire->ownerRecord);
}
)
->searchable(
query: function(Builder $query, string $search, RelationManager $livewire) {
$class = new TeamProfileItemsRelationManager;
return $class->getDescriptionSearchQuery($search, $livewire->ownerRecord);
}
)
I believe this is a MYSQL limitation, not a Filament bug my custom query adds the appropriate where clauses and when I actually use get() and log the result, the data is correctly filtered however, I cannot get searchable() to use that correctly filtered data any ideas about what is going on here?
cheesegrits
cheesegrits15mo ago
Do you have a help thread that details your custom RM query?
bwurtz999
bwurtz99915mo ago
I don't But I have verified that it is pulling the correct data
bwurtz999
bwurtz99915mo ago
What's strange is that it doesn't appear to be filtering anything at all Should I start a separate thread?
cheesegrits
cheesegrits15mo ago
Yes please. So we don't spam OP with your stuff.
Want results from more Discord servers?
Add your server