Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup globally searchable relationships #25

Merged
merged 1 commit into from Oct 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
31 changes: 17 additions & 14 deletions readme.md
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
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