Skip to main content
Tutorial Free

Filament: Search in Table with Accessor Column

April 08, 2024
1 min read

If you use Eloquent Accessor in the Filament table, how do you make it searchable? Let me show you an example.

For example, you have users.first_name and users.last_name DB columns:

And you want to display them together as "Full name" in the Filament table column.

That part is easy. Filament will automatically recognize the Eloquent Accessor:

app/Models/User.php:

class User extends Authenticatable
{
// ...
 
public function getFullNameAttribute()
{
return $this->first_name . ' ' . $this->last_name;
}
}

app/Filament/Resources/UserResource.php:

public static function table(Table $table): Table
{
return $table
->columns([
Tables\Columns\TextColumn::make('full_name'),
Tables\Columns\TextColumn::make('email'),
Tables\Columns\TextColumn::make('created_at'),
]);

It works!

But try to use ->searchable() on that column:

TextColumn::make('full_name')->searchable(),

It will throw an error after the search:

The solution is to pass all the related columns in the parameter array of the searchable() method:

TextColumn::make('full_name')->searchable([
'first_name', 'last_name'
]),

And now the search works!

Read more in the official Filament docs.


If you want more Filament examples, you can find more real-life projects on our FilamentExamples.com.

Enjoyed This Tutorial?

Get access to all premium tutorials, video and text courses, and exclusive Laravel resources. Join our community of 10,000+ developers.

Comments & Discussion

MA
Mohammad Al Amin ✓ Link copied!

What if we want to use individual search on that "Full Name" column like the "->searchable(isIndividual: true)" How would we pass the related columns(first_name, last_name) to the searchable() method and also set the individual search functionality to true.?? I was trying to find a solution to that but couldn't get it done even with the help of copilot.

M
Modestas ✓ Link copied!

I'm not sure what the issue is here, but this should work:

Tables\Columns\TextColumn::make('first_name')
->searchable(['first_name', 'last_name'], isIndividual: true),

Since all you do with both cases - pass parameters to the function. And the isIndividual: true uses PHP functionality to pass named parameter in any order.

MA
Mohammad Al Amin ✓ Link copied!

Thanks so much, it actually worked. Earlier i was writing it as ->searchable(isIndividual: true, ['first_name', 'last_name']) , which gives an error because it says we cannot use positional args after named agrs. I'm not sure why Copilot couldn't catch or fix that..🤔 Thanks again.

M
Modestas ✓ Link copied!

Because copilot is pretty dumb :) It does not care if it works or not, it does not always have the latest knowledge.

So while it can help you write code - it might not be the best :) And this was the case right here - it can write it, but it has no idea how it should actually be. Just be careful with it ;)

We'd Love Your Feedback

Tell us what you like or what we can improve

Feel free to share anything you like or dislike about this page or the platform in general.