Searching and filtering data in web applications is a common requirement for developers. In FilamentPHP applications, you can achieve this efficiently with the help of Eloquent ORM, which simplifies the development of admin panels and resource tables. In this blog post, we will explore how to extend the functionality of the Filament resource table search by allowing users to search for data across non-table columns. We'll do this by adding a custom search function to your Filament resource table.

Understanding Filament's Resource Table Search

Filament is a popular Laravel package that makes it easy to create beautiful admin panels and resource tables for your application. The resource table component in Filament allows you to display and manage database records in a user-friendly manner.

To enable search functionality in a Filament resource table, you need to apply ->searchable() with every table column. Right?. Imagine, your client needs to search the table records which we are not showing under the table. i.e, clients need to search records which is stored on another table or other scenarios.

In Filament, on the List Page, you can use the applySearchToTableQuery method. This method is used to apply search filters to the database query builder, allowing users to search for records in the resource table.

In the provided code snippet, you can see the applySearchToTableQuery function, which includes a call to applyColumnSearchesToTableQuery to search in table columns. But what if you want to expand the search capability to non-table columns or add custom search logic?

 
protected function applySearchToTableQuery(Builder $query): Builder
{
        $this->applyColumnSearchesToTableQuery($query);
         
        if (filled($search = $this->getTableSearch())) {
            $query->where("column_name","like","%$search%"); 
        }

        return $query;
}

The above code snippet demonstrates how you can achieve this. Here's a breakdown of the key components:

  • applyColumnSearchesToTableQuery: This is the built-in Filament method for searching within the columns of your database table. It allows users to search for records based on the values in these columns.
  • Custom Search Logic: To extend the search functionality to non-table columns, you can add a custom condition within the if (filled($search)) block. In this example, we are searching for records where the "column_name" matches the search query. You can replace "column_name" with the name of your non-table column.

With this setup, you can search not only within table columns but also in any additional data you want to make searchable in your resource table.