TitasGailius / nova-search-relations by TitasGailius

A Laravel Nova tool.
5,131,150
344
5
Package Data
Maintainer Username: TitasGailius
Package Create Date: 2018-09-01
Package Last Update: 2023-03-22
Home Page:
Language: PHP
License: MIT
Last Refreshed: 2024-04-26 03:03:08
Package Statistics
Total Downloads: 5,131,150
Monthly Downloads: 94,249
Daily Downloads: 3,983
Total Stars: 344
Total Watchers: 5
Total Forks: 33
Total Open Issues: 5

Search relationships in Laravel Nova

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

Screenshot

screenshot of the search relations tool

Installation

composer require titasgailius/search-relations

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

use Titasgailius\SearchRelations\SearchesRelations;

abstract class Resource extends NovaResource
{
    use SearchesRelations;

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.

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

Global search

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

/**
 * 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:

/**
 * 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.

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