Skip to content

Commit

Permalink
Merge pull request #25 from TitasGailius/feature/cleanup-global-search
Browse files Browse the repository at this point in the history
Cleanup globally searchable relationships
  • Loading branch information
TitasGailius authored Oct 9, 2020
2 parents 1dc7b40 + e39accb commit ac999c8
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 17 deletions.
31 changes: 17 additions & 14 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand Down
33 changes: 30 additions & 3 deletions src/SearchesRelations.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down

0 comments on commit ac999c8

Please sign in to comment.