PHPackages                             workup/search-relations - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Search &amp; Filtering](/categories/search)
4. /
5. workup/search-relations

ActiveLibrary[Search &amp; Filtering](/categories/search)

workup/search-relations
=======================

A Laravel Nova tool.

2.0.1.001(3y ago)0322MITPHPPHP &gt;=7.1.0

Since Sep 1Pushed 3y agoCompare

[ Source](https://github.com/workupsrl/nova-search-relations)[ Packagist](https://packagist.org/packages/workup/search-relations)[ RSS](/packages/workup-search-relations/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (1)DependenciesVersions (11)Used By (0)

Search relationships in Laravel Nova
====================================

[](#search-relationships-in-laravel-nova)

This package allows you to include relationship columns into Laravel Nova search query.

Screenshot
----------

[](#screenshot)

[![screenshot of the search relations tool](./screenshot.png)](./screenshot.png)

Installation
------------

[](#installation)

```
composer require workup/search-relations

```

Next, add `Workup\SearchRelations\SearchesRelations` trait to your base resource class `App\Nova\Resource`

```
use Workup\SearchRelations\SearchesRelations;

abstract class Resource extends NovaResource
{
    use SearchesRelations;
```

Usage
-----

[](#usage)

Simply add `public static $searchRelations` variable to any of your Nova resources. This array accepts a relationship name as a key and an array of searchable columns as a value.

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

Alternatively, you may add a static `searchableRelations()` method to return an array of searchable relations.

```
/**
 * Get the searchable columns for the resource.
 *
 * @return array
 */
public static function searchableRelations(): array
{
    return [
        'user' => ['username', 'email'],
    ];
}
```

Global search
-------------

[](#global-search)

You may customize the rules of your searchable relationships for global search by defining the `$globalSearchRelations` property.

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

Alternatively, you may add a static `globallySearchableRelations()` method to return an array of globally searchable relations.

```
/**
 * Get the searchable columns for the resource.
 *
 * @return array
 */
public static function globallySearchableRelations(): array
{
    return [
        'user' => ['email'],
    ];
}
```

---

#### Disabling global search for relationships

[](#disabling-global-search-for-relationships)

You may disable the global relationship search by declaring `$globalSearchRelations` with an empty array.

```
/**
 * The relationship columns that should be searched globally.
 *
 * @var array
 */
public static $globalSearchRelations = [];
```

Alternatevily, you may disable the global search for relationships by setting the `$searchRelationsGlobally` property to `false`.

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

Nested relationships
--------------------

[](#nested-relationships)

You may search nested relationships using dot notation.

```
/**
 * The relationship columns that should be searched.
 *
 * @var array
 */
public static $searchRelations = [
    'user.country' => ['code'],
];
```

Extending Search
----------------

[](#extending-search)

You may apply custom search logic for the specified relations by retuning a class implementing a `Search` interface.

```
/**
 * Get the searchable columns for the resource.
 *
 * @return array
 */
public static function searchableRelations(): array
{
    return [
        'country' => new LocationSearch(['USA', 'UK']),
    ];
}
```

Your custom search class must implement a simple `Search` interface that has a single method which accepts the current query `$query`, a relationship name `$relation` and a search input `$search`.

```
