The Basics
Learn how to define searchable columns for your Nova resources.
Searchable Columns
To define which resource fields are searchable, you may assign an array of database columns to the search
property or searchableColumns
method of your resource class. This array includes the id
column by default:
If you are using Nova’s Scout integration, the $search
property has no effect on your search results and may be ignored. You should manage the searchable columns within the Algolia or Meilisearch dashboard.
Full-Text Indexes
Typically, Nova searches your database columns using simple LIKE
clauses. However, if you are using MySQL or Postgres, you may take advantage of any full-text indexes you have defined. To do so, you should define a searchableColumns
method on your Nova resource class instead of defining a $search
property.
The searchableColumns
method should return an array of columns that are searchable. You may include an instance of Laravel\Nova\Query\Search\SearchableText
within this array to instruct Nova to utilize your full-text indexes when querying a given column:
The array returned by the searchableColumns
method can also include raw SQL expressions, which allow you to search through derived columns:
Searching Relationships
Laravel Nova also allows you to search against a resource’s related models. For example, imagine a Post
model that is related to a User
model via an author
relatonship. You may indicate that this relationship data should be considered when searching for users by returning an instance of Laravel\Nova\Query\Search\SearchableRelation
from your resource’s searchableColumns
method.
If the searchableColumns
method does not exist on your resource, you should define it. Once the searchableColumns
method has been defined, you may remove the $search
property from your resource:
For convenience, you may define a relationship that should be searched by adding the field to your resource’s $search
property using “dot notation”:
MorphTo Relationships
“Morph to” relationships can be made searchable by returning an instance of Laravel\Nova\Query\Search\SearchableMorphToRelation
from your resource’s searchableColumns
method. The SearchableMorphToRelation
class allows you to specify which types of morphed models should be searched:
Searching JSON Data
If the database table associated with your resource includes a column that contains a JSON string, you may instruct Nova to search within the JSON string by returning a Laravel\Nova\Query\Search\SearchableJson
instance from your resource’s searchableColumns
method.
If the searchableColumns
method does not exist on your resource, you should define it. Once the searchableColumns
method has been defined, you may remove the $search
property from your resource:
Was this page helpful?