Skip to main content

Column Sort, Search and Validate

Premium
4:48

Text Version of the Lesson

In this 3-in-1 lesson, let's talk about the most common operations we can do with Filament columns/fields:

  • Validating fields
  • Sort columns in the table
  • Search by table column

I decided to group those into one lesson because they are so easily accomplished in Filament that the lesson will be pretty short, anyway.


Validating Fields in Filament

If you want to add behavior to form fields, you can chain multiple methods to the XxxxxColumn::make() method.

One of the most widely used ones would be ->required() for the validation.

return $form
->schema([
Forms\Components\TextInput::make('name')->required(),
Forms\Components\TextInput::make('price')->required(),
]);

Just by adding this, we have this behavior in the create/edit forms: validation error and asterisk sign near the label.

And there are more Filament methods for validation that you can chain together.

Forms\Components\TextInput::make('name')
->required()
->unique(),

Notice: I start putting those methods on separate lines for better readability.

Some Filament validation methods have extra...

The Full Lesson is Only for Premium Members

Want to access all of our courses? (30 h 09 min)

You also get:

55 courses
Premium tutorials
Access to repositories
Private Discord
Get Premium for $129/year or $29/month

Already a member? Login here

Comments & Discussion

QP
Quentin Pain ✓ Link copied!

Super powerful and so simple. Thanks for this.

E
Emmanuel71 ✓ Link copied!

Thanks for the nice work! Is it possible to provide a custom search query for the field with a closure? Looking at the definition of the searchable method, I'd think it's possible, but I can't seem to find any examples of this.

M
Modestas ✓ Link copied!

You can add closure there:

TextColumn::make('full_name')
->searchable(query: function (Builder $query, string $search): Builder {
return $query
->where('first_name', 'like', "%{$search}%")
->orWhere('last_name', 'like', "%{$search}%");
})

https://filamentphp.com/docs/3.x/tables/columns/getting-started#searching

E
Emmanuel71 ✓ Link copied!

This is what I was looking for, and it works. Thanks!

C
C4 ✓ Link copied!

What can I do for methods that require a relationship instance? For example my model contains this method:

public function highestBid() { return $this->offers()->orderBy('bid_amount', 'desc')->first(); }

it can only be accessed like $model->highestBid()->bid_amount. While I could do some query builder I have other relationships across the app that require it to be used with parentheses. I cannot figure out how to list it in a Filament Table, it appears to return null. I've tried:

Tables\Columns\TextColumn::make('highest_bid') ->label('Highest Bid') ->relationship(name: 'highestBid', titleAttribute: 'bid_amount')

Tables\Columns\TextColumn::make('highest_bid') ->label('Highest Bid') ->formatStateUsing(function ($record) { return $record->highestBid()->bid_amount ?? 'N/A'; }),

  • a RelationManager. To no avail, there must be a way I'm overlooking.

In addition to this issue, I also cannot figure out how to eager load 3 levels deep for this specific column. Tables\Columns\TextColumn::make('item.product.name') ->label('Product'),

Any chance I can get a brainiac to assist? Thanks

M
Modestas ✓ Link copied!

To get a relationship - the name has to match exactly. In your case highest_bid should be highestBid and that would work.

Loading 3 levels is something different, but in theory - it should work the same. If it doesn't, you can use a custom column or formatStateUsing() on the column and do what you need to do there :)

C
C4 ✓ Link copied!

Interesting. Thanks for the speedy reply! Once I get my coffee Ima give this a hoot.

Tracked my errors back to /vendor/laravel/framework/src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php:586

My highestBid function is is not a standard Eloquent relationship but rather a custom method that returns a specific value. Creating this into a proper attribuite works. So I made new method for now since the original method has ties to jobs/etc.

public function getHighestBidfilamentAttribute() { return $this->offers()->orderBy('bid_amount', 'desc')->first()->bid_amount; }

Figure I share incase there is anyone else suffering from the occasional brainfart like me.

Also incredible response time, wasn't really exepecting one, given I don't post here ever. Incredible team, LaravelDaily to the MOON! Many thanks

M
Modestas ✓ Link copied!

Thank you for the nice words! We try to reply as soon as possible as that is part of the membership people get! (we are even faster on discord :) ). The only time we can be slow - is the weekend :)

By what you wrote - it makes sense. You do need to have a relationship for most filament things to function properly. So if you were able to make it work with a relationship - that's awesome!

M
marcelo-kazalukian ✓ Link copied!

Hello, excellent course.

I have a problem though, Filament shows me a blank page when I click on the column title to sort it. I followed this tutorial and the official filament example (about a system for vets) and same problem.

When I go to http://daily.test/admin/products I can see the table, then when I click on the colum "name" for example, the url changes to http://daily.test/admin/products?tableSortColumn=name&tableSortDirection=asc but the page is black (I'm using dark mode), and the cursor mouse is like cursor-pointer in the whole page.

Then if I copy and paste this url http://daily.test/admin/products?tableSortColumn=name&tableSortDirection=asc in the browser and press enter everything is ok, but if I cllick on the column name I got the same error.

I'm using a new Laravel 11 project and I installed Filament using composer require filament/filament:"^3.2.57-stable" -W

I googled a lot but no luck.

Any suggestion about this problem?

Thanks so much

M
Modestas ✓ Link copied!

I would start to debug this by:

  1. Looking at browsers console - maybe some kind of an error happens there?
  2. Trying to run composer update to get latest versions of all packages
  3. Running php artisan cache:clear to clear up cached views (sometimes it doesn't trigger automatically)

But other than that - this is a really specific issue, which can be either a simple code mistake or an issue in the code. Hard to debug without the actual code and in a comment :)

M
marcelo-kazalukian ✓ Link copied!

Thanks for the response.

After many attempts and googling, I found the solution here https://github.com/filamentphp/filament/issues/13360

If you are using the latest Laravel version (06/01/2025) and you can't install filament using the command from the official website (composer require filament/filament:"^3.2" -W) use

composer require filament/filament -W

Thanks so much.

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.