Skip to content

Commit

Permalink
Separate global search
Browse files Browse the repository at this point in the history
  • Loading branch information
TitasGailius committed Dec 18, 2018
1 parent 743f196 commit 1e7408a
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
50 changes: 50 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,53 @@ public static $searchRelations = [
'user' => ['username', 'email'],
];
```

## Global search

You may disable global search for relationship columns by defining `$searchRelationsGlobally` property in your nova resource:

```php
/**
* Determine if relations should be searched globally.
*
* @var array
*/
public static $searchRelationsGlobally = false;
```

When you have disabled global search for relationships, you may still enable it for specific relationships like this:
```php
/**
* Determine if relations should be searched globally.
*
* @var array
*/
public static $searchRelationsGlobally = false;

/**
* The relationship columns that should be searched globally.
*
* @var array
*/
public static $globalSearchRelations = [
'user' => ['email'],
];

```

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.

```php
/**
* The relationship columns that should be searched.
*
* @var array
*/
public static $searchRelations = [
'user.country' => ['code'],
];
```
16 changes: 16 additions & 0 deletions src/SearchesRelations.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,25 @@ public static function searchable()
*/
public static function searchableRelations(): array
{
$searchRelationsGlobally = static::$searchRelationsGlobally ?? true;

if (!$searchRelationsGlobally && static::isGlobalSearch()) {
return static::$globalSearchRelations ?? [];
}

return static::$searchRelations ?? [];
}

/**
* Determine whether current request is for global search.
*
* @return boolean
*/
protected static function isGlobalSearch()
{
return request()->route()->action['uses'] === 'Laravel\Nova\Http\Controllers\SearchController@index';
}

/**
* Apply the search query to the query.
*
Expand Down

0 comments on commit 1e7408a

Please sign in to comment.