- Notifications
You must be signed in to change notification settings - Fork37
This package allow you to include relationship columns into Laravel Nova search query.
License
TitasGailius/nova-search-relations
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
I stand with Ukraine | 🇺🇦 |
---|
This package allows you to include relationship columns into Laravel Nova search query.
composer require titasgailius/search-relations
Next, addTitasgailius\SearchRelations\SearchesRelations
trait to your base resource classApp\Nova\Resource
useTitasgailius\SearchRelations\SearchesRelations;abstractclass Resourceextends NovaResource{use SearchesRelations;
Simply addpublic 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 */publicstatic$searchRelations = ['user' => ['username','email'],];
Alternatively, you may add a staticsearchableRelations()
method to return an array of searchable relations.
/** * Get the searchable columns for the resource. * * @return array */publicstaticfunctionsearchableRelations():array{return ['user' => ['username','email'], ];}
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 */publicstatic$globalSearchRelations = ['user' => ['email'],];
Alternatively, you may add a staticgloballySearchableRelations()
method to return an array of globally searchable relations.
/** * Get the searchable columns for the resource. * * @return array */publicstaticfunctiongloballySearchableRelations():array{return ['user' => ['email'], ];}
You may disable the global relationship search by declaring$globalSearchRelations
with an empty array.
/** * The relationship columns that should be searched globally. * * @var array */publicstatic$globalSearchRelations = [];
Alternatevily, you may disable the global search for relationships by setting the$searchRelationsGlobally
property tofalse
.
/** * Determine if relations should be searched globally. * * @var array */publicstatic$searchRelationsGlobally =false;
You may search nested relationships using dot notation.
/** * The relationship columns that should be searched. * * @var array */publicstatic$searchRelations = ['user.country' => ['code'],];
You may apply custom search logic for the specified relations by retuning a class implementing aSearch
interface.
/** * Get the searchable columns for the resource. * * @return array */publicstaticfunctionsearchableRelations():array{return ['country' =>newLocationSearch(['USA','UK']), ];}
Your custom search class must implement a simpleSearch
interface that has a single method which acceptsthe current query$query
, a relationship name$relation
and a search input$search
.
<?phpnamespaceTitasgailius\SearchRelations\Contracts;useIlluminate\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 */publicfunctionapply(Builder$query,string$relation,string$search):Builder;}
You may take a look at theTitasgailius\SearchRelations\Searches\RelationSearch
class as an example.
About
This package allow you to include relationship columns into Laravel Nova search query.