How to Use Laravel Nova Search Relations

How to Use Laravel Nova Search Relations. This package allows you to include relationship columns into Laravel Nova search query use novapackages.

Contents
Searchable Columns
To define which resource fields are searchable, you may assign an array of database columns to the search
property of your resource class. This array includes the id
column by default:
/**
* The columns that should be searched.
*
* @var array
*/
public static $search = [
'id', 'title', 'content',
];
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:
use Laravel\Nova\Query\Search\SearchableText;
/**
* Get the searchable columns for the resource.
*
* @return array
*/
public static function searchableColumns()
{
return ['id', new SearchableText('title')];
}
#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:
use Laravel\Nova\Query\Search\SearchableRelation;
/**
* Get the searchable columns for the resource.
*
* @return array
*/
public static function searchableColumns()
{
return ['id', new SearchableRelation('author', 'name')];
}
For convenience, you may define a relationship that should be searched by adding the field to your resource’s $search
property using “dot notation”:
/**
* The columns that should be searched.
*
* @var array
*/
public static $search = [
'id', 'author.name'
];
#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:
use App\Nova\Post;
use Laravel\Nova\Query\Search\SearchableMorphToRelation;
/**
* Get the searchable columns for the resource.
*
* @return array
*/
public static function searchableColumns()
{
return ['id', new SearchableMorphToRelation('commentable', 'title', [Post::class])];
}
#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:
use Laravel\Nova\Query\Search\SearchableJson;
/**
* Get the searchable columns for the resource.
*
* @return array
*/
public static function searchableColumns()
{
return ['id', new SearchableJson('meta->address->postcode')];
}
Installation Instructions
composer require titasgailius/search-relations
Next, add Titasgailius\SearchRelations\SearchesRelations
trait to your base resource class App\Nova\Resource
use Titasgailius\SearchRelations\SearchesRelations;
abstract class Resource extends NovaResource
{
use SearchesRelations;
Usage
How to Use Laravel Nova Search Relations
Simply add public static $searchRelations
array to any of your Nova resources. This array has a relationship name as a key and an array of columns to search for as a value.
/**
* The relationship columns that should be searched.
*
* @var array
*/
public static $searchRelations = [
'user' => ['username', 'email'],
];
Readme
I stand with Ukraine | 🇺🇦 |
---|
Search relationships in Laravel Nova
This package allows you to include relationship columns into Laravel Nova search query.
Screenshot

Installation
composer require titasgailius/search-relations
Next, add Titasgailius\SearchRelations\SearchesRelations
trait to your base resource class App\Nova\Resource
use Titasgailius\SearchRelations\SearchesRelations;
abstract class Resource extends NovaResource
{
use SearchesRelations;
Usage
Simply add public static $searchRelations
variable to any of your Nova resources. This array accepts a relationship name as a key and an array of searchable columns as a value.
/**
* The relationship columns that should be searched.
*
* @var array
*/
public static $searchRelations = [
'user' => ['username', 'email'],
];
Alternatively, you may add a static searchableRelations()
method to return an array of searchable relations.
/**
* Get the searchable columns for the resource.
*
* @return array
*/
public static function searchableRelations(): array
{
return [
'user' => ['username', 'email'],
];
}
Global search
You may customize the rules of your searchable relationships for global search by defining the $globalSearchRelations
property.
/**
* The relationship columns that should be searched globally.
*
* @var array
*/
public static $globalSearchRelations = [
'user' => ['email'],
];
Alternatively, you may add a static globallySearchableRelations()
method to return an array of globally searchable relations.
/**
* Get the searchable columns for the resource.
*
* @return array
*/
public static function globallySearchableRelations(): array
{
return [
'user' => ['email'],
];
}
Disabling global search for relationships
You may disable the global relationship search by declaring $globalSearchRelations
with an empty array.
/**
* The relationship columns that should be searched globally.
*
* @var array
*/
public static $globalSearchRelations = [];
Alternatevily, you may disable the global search for relationships by setting the $searchRelationsGlobally
property to false
.
/**
* Determine if relations should be searched globally.
*
* @var array
*/
public static $searchRelationsGlobally = false;
Nested relationships
You may search nested relationships using dot notation.
/**
* The relationship columns that should be searched.
*
* @var array
*/
public static $searchRelations = [
'user.country' => ['code'],
];
Extending Search
You may apply custom search logic for the specified relations by retuning a class implementing a Search
interface.
/**
* Get the searchable columns for the resource.
*
* @return array
*/
public static function searchableRelations(): array
{
return [
'country' => new LocationSearch(['USA', 'UK']),
];
}
Your custom search class must implement a simple Search
interface that has a single method which accepts the current query $query
, a relationship name $relation
and a search input $search
.
<?php
namespace Titasgailius\SearchRelations\Contracts;
use Illuminate\Database\Eloquent\Builder;
interface Search
{
/**
* Apply search for the given relation.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @param string $relation
* @param string $search
* @return \Illuminate\Database\Eloquent\Builder
*/
public function apply(Builder $query, string $relation, string $search): Builder;
}
You may take a look at the Titasgailius\SearchRelations\Searches\RelationSearch
 class as an example.
Source : https://github.com/TitasGailius/nova-search-relations
Keywords:
- novapackages
- Laravel nova
- Laravel nova search relations
- Laravel nova search relations novapackages