Skip to content

Commit ac999c8

Browse files
authored
Merge pull request #25 from TitasGailius/feature/cleanup-global-search
Cleanup globally searchable relationships
2 parents 1dc7b40 + e39accb commit ac999c8

File tree

2 files changed

+47
-17
lines changed

2 files changed

+47
-17
lines changed

readme.md

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ abstract class Resource extends NovaResource
2424
## Usage
2525

2626
Simply add `public static $searchRelations` array to any of your Nova resources.
27-
This array has a relationship name as a key and an array of columns to search for as a value.
27+
This array accepts a relationship name as a key and an array of searchable columns as a value.
28+
2829
```php
2930
/**
3031
* The relationship columns that should be searched.
@@ -38,39 +39,41 @@ public static $searchRelations = [
3839

3940
## Global search
4041

41-
You may disable global search for relationship columns by defining `$searchRelationsGlobally` property in your nova resource:
42+
You may customize the rules of your searchable relationships in global search by defining the `$globalSearchRelations` property.
4243

4344
```php
4445
/**
45-
* Determine if relations should be searched globally.
46+
* The relationship columns that should be searched globally.
4647
*
4748
* @var array
4849
*/
49-
public static $searchRelationsGlobally = false;
50+
public static $globalSearchRelations = [
51+
'user' => ['email'],
52+
];
5053
```
5154

52-
When you have disabled global search for relationships, you may still enable it for specific relationships like this:
55+
You may disable the global search for relationships by defining the `$globalSearchRelations` property with an empty array.
56+
5357
```php
5458
/**
55-
* Determine if relations should be searched globally.
59+
* The relationship columns that should be searched globally.
5660
*
5761
* @var array
5862
*/
59-
public static $searchRelationsGlobally = false;
63+
public static $globalSearchRelations = [];
64+
```
65+
66+
Alternatevily, you may disable the global search for relationships by setting the `$searchRelationsGlobally` property to `false`.
6067

68+
```php
6169
/**
62-
* The relationship columns that should be searched globally.
70+
* Determine if relations should be searched globally.
6371
*
6472
* @var array
6573
*/
66-
public static $globalSearchRelations = [
67-
'user' => ['email'],
68-
];
69-
74+
public static $searchRelationsGlobally = false;
7075
```
7176

72-
Now when searching globally, Laravel Nova is going to **ignore** relationships declared in `$searchRelations` and is going to use `$globalSearchRelations` instead.
73-
7477
## Nested relationships
7578

7679
You may search nested relationships using dot notation.

src/SearchesRelations.php

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,42 @@ public static function searchable()
2424
*/
2525
public static function searchableRelations(): array
2626
{
27-
$searchRelationsGlobally = static::$searchRelationsGlobally ?? true;
27+
if (static::isGlobalSearch()) {
28+
return static::globallySearchableRelations();
29+
}
2830

29-
if (!$searchRelationsGlobally && static::isGlobalSearch()) {
30-
return static::$globalSearchRelations ?? [];
31+
return static::$searchRelations ?? [];
32+
}
33+
34+
/**
35+
* Get the globally searchable relations for the resource.
36+
*
37+
* @return array
38+
*/
39+
public static function globallySearchableRelations(): array
40+
{
41+
if (isset(static::$globalSearchRelations)) {
42+
return static::$globalSearchRelations;
43+
}
44+
45+
if (static::globalSearchDisabledForRelations()) {
46+
return [];
3147
}
3248

3349
return static::$searchRelations ?? [];
3450
}
3551

52+
/**
53+
* Determine if a global search is disabled for the relationships.
54+
*
55+
* @return boolean
56+
*/
57+
protected static function globalSearchDisabledForRelations(): bool
58+
{
59+
return isset(static::$searchRelationsGlobally)
60+
&& ! static::$searchRelationsGlobally;
61+
}
62+
3663
/**
3764
* Determine whether current request is for global search.
3865
*

0 commit comments

Comments
 (0)