F
Filamentβ€’4mo ago
Damien

How do I make a table column searchable based on its state value?

I have a table column that has a default value of a user_id. However, I format the state to show the users first and last name. Here is the code I have for doing so:
TextColumn::make('client_id')
->label('Client')
->searchable() // TODO: works for id but not the formatted name value ????
->formatStateUsing(function ($state) {
$contact = Contact::where('id', $state)->first();

return $contact->first_name.' '.$contact->last_name;
}),
TextColumn::make('client_id')
->label('Client')
->searchable() // TODO: works for id but not the formatted name value ????
->formatStateUsing(function ($state) {
$contact = Contact::where('id', $state)->first();

return $contact->first_name.' '.$contact->last_name;
}),
If I search using the user id, it works, however what I would like to be able to do is search by the value of the users name. Is there a way to do this with my current code or do I need to do something differently?
8 Replies
Damien
Damienβ€’4mo ago
Anyone able to help point me in the right direction for this? It would be much appreciated.
ModestasV
ModestasVβ€’4mo ago
This might help you: https://filamentphp.com/docs/3.x/tables/columns/getting-started#searching Especially this part:
->searchable(query: function (Builder $query, string $search): Builder {
return $query
->where('first_name', 'like', "%{$search}%")
->orWhere('last_name', 'like', "%{$search}%");
})
->searchable(query: function (Builder $query, string $search): Builder {
return $query
->where('first_name', 'like', "%{$search}%")
->orWhere('last_name', 'like', "%{$search}%");
})
Damien
Damienβ€’4mo ago
Thank you Modestas, I will have a look at this now! So this would work if I was searching on the contact resource but I am searching on a different resource where the default value is the id of the contact and not the name values. This is why I use the formateStateUsing method and not sure how to make the above work with values from another table.
ChesterS
ChesterSβ€’4mo ago
What exactly are you trying to achieve here? Are you loading the data (first_name + last_name) from a relationship? If yes, there are better ways to do this. Either way, Modestas' advice still applies, you just have to change the query - which will be a bit of a custerfuck to make it work with your current table.
ModestasV
ModestasVβ€’4mo ago
It doesn't really matter if it's current or related table πŸ™‚ What matters is the SQL query you will write. In your case, you have to specifically define the table/relationship you are searching in
Damien
Damienβ€’4mo ago
My apologies for misunderstanding and potentially not explaining myself clearly enough. My end goal is to be able to search for a clients name (first_name/last_name) on my project resource table.
What exactly are you trying to achieve here? Are you loading the data (first_name + last_name) from a relationship? If yes, there are better ways to do this.
At the moment no, the id value of the client is returned and then I formateStateUsing the id to get the client details and render the name.
It doesn't really matter if it's current or related table πŸ™‚ What matters is the SQL query you will write. In your case, you have to specifically define the table/relationship you are searching in
I do have a relationship defined but I will assume it is wrong somewhere as it is not working correctly.
LeandroFerreira
LeandroFerreiraβ€’4mo ago
You should fix your relationship. Additionally, you can use a virtual column to define a full_name column $table->string('full_name')->virtualAs('concat(first_name, \' \', last_name)');
Tables\Columns\TextColumn::make('full_name')
->searchable()
->sortable()
Tables\Columns\TextColumn::make('full_name')
->searchable()
->sortable()
Damien
Damienβ€’4mo ago
I will look at this, thank you Leandro This is working now and looks like this:
TextColumn::make('client.full_name')
->label('Client')
->sortable()
->searchable(),
TextColumn::make('client.full_name')
->label('Client')
->sortable()
->searchable(),
Thank you all
Want results from more Discord servers?
Add your server
More Posts