From e39accb8b5b8cd9ffa794d3a4795f6c3623558d5 Mon Sep 17 00:00:00 2001 From: Titas Gailius Date: Fri, 9 Oct 2020 12:22:04 +0300 Subject: [PATCH] Cleanup globally searchable relationships --- readme.md | 31 +++++++++++++++++-------------- src/SearchesRelations.php | 33 ++++++++++++++++++++++++++++++--- 2 files changed, 47 insertions(+), 17 deletions(-) diff --git a/readme.md b/readme.md index 0f78b9c..136aca9 100644 --- a/readme.md +++ b/readme.md @@ -24,7 +24,8 @@ abstract class Resource extends NovaResource ## Usage 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. +This array accepts a relationship name as a key and an array of searchable columns as a value. + ```php /** * The relationship columns that should be searched. @@ -38,39 +39,41 @@ public static $searchRelations = [ ## Global search -You may disable global search for relationship columns by defining `$searchRelationsGlobally` property in your nova resource: +You may customize the rules of your searchable relationships in global search by defining the `$globalSearchRelations` property. ```php /** - * Determine if relations should be searched globally. + * The relationship columns that should be searched globally. * * @var array */ -public static $searchRelationsGlobally = false; +public static $globalSearchRelations = [ + 'user' => ['email'], +]; ``` -When you have disabled global search for relationships, you may still enable it for specific relationships like this: +You may disable the global search for relationships by defining the `$globalSearchRelations` property with an empty array. + ```php /** - * Determine if relations should be searched globally. + * The relationship columns that should be searched globally. * * @var array */ -public static $searchRelationsGlobally = false; +public static $globalSearchRelations = []; +``` + +Alternatevily, you may disable the global search for relationships by setting the `$searchRelationsGlobally` property to `false`. +```php /** - * The relationship columns that should be searched globally. + * Determine if relations should be searched globally. * * @var array */ -public static $globalSearchRelations = [ - 'user' => ['email'], -]; - +public static $searchRelationsGlobally = false; ``` -Now when searching globally, Laravel Nova is going to **ignore** relationships declared in `$searchRelations` and is going to use `$globalSearchRelations` instead. - ## Nested relationships You may search nested relationships using dot notation. diff --git a/src/SearchesRelations.php b/src/SearchesRelations.php index 6db38d4..840918b 100644 --- a/src/SearchesRelations.php +++ b/src/SearchesRelations.php @@ -24,15 +24,42 @@ public static function searchable() */ public static function searchableRelations(): array { - $searchRelationsGlobally = static::$searchRelationsGlobally ?? true; + if (static::isGlobalSearch()) { + return static::globallySearchableRelations(); + } - if (!$searchRelationsGlobally && static::isGlobalSearch()) { - return static::$globalSearchRelations ?? []; + return static::$searchRelations ?? []; + } + + /** + * Get the globally searchable relations for the resource. + * + * @return array + */ + public static function globallySearchableRelations(): array + { + if (isset(static::$globalSearchRelations)) { + return static::$globalSearchRelations; + } + + if (static::globalSearchDisabledForRelations()) { + return []; } return static::$searchRelations ?? []; } + /** + * Determine if a global search is disabled for the relationships. + * + * @return boolean + */ + protected static function globalSearchDisabledForRelations(): bool + { + return isset(static::$searchRelationsGlobally) + && ! static::$searchRelationsGlobally; + } + /** * Determine whether current request is for global search. *