라라벨 노바는 리소스 검색시 릴레이션 테이블에서 검색하는 기능을 제공하지 않습니다. 

nova search relationship 는 이런 문제를 해결해주는 패키지로 Nova Resource 검색시 연관 테이블에서도 검색이 가능합니다.

설치

컴포저로 의존성을 설치합니다.

composer require titasgailius/search-relations
BASH


trait Titasgailius\SearchRelations\SearchesRelations 을 노바 기본 리소스에 추가합니다.

use Titasgailius\SearchRelations\SearchesRelations;

abstract class Resource extends NovaResource
{
    use SearchesRelations;
CODE


사용

Relation 검색을 하려는 Nova Resource 클래스에 $searchRelations property에 검색할 relation 과 컬럼을 선언해 줍니다.

배열의 key(Ex: user) 는 Model Class 에 정의한 relation 의 이름입니다. 즉 아래 예제는 Post 모델 클래스에 user() 라는 메서드가 정의되어야 합니다.

<?php

class Post extends Resource
{
	public static $model = \App\Models\Post::class;
	
	/** 
	* The relationship columns that should be searched. 
	* * @var array 
	*/ 
	public static $searchRelations = [ 
		'user' => [
			'username', 
			'email',
		], 
	];
PHP


property 대신 searchableRelations() 메서드를 구현해 주어도 동일하게 동작합니다.

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


global search

Nova 의 글로벌 search 시 relation 을 같이 검색하려면 $globalSearchRelations  프로퍼티를 정의해 줍니다.

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

또는 globallySearchableRelations() 메서드를 구현해도 됩니다.


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

PHP


disable global search

global search 에서 제외하려면 $globalSearchRelations array 를 빈 값으로 설정합니다.

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


또는 $searchRelationsGlobally  프로퍼티를 false 로 설정해도 됩니다.

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

Nested relation

relation 모델에서 또 다른 모델을 검색하려면 dot notation 으로 중첩해서 기술해 주면 됩니다. 

예로 다음 코드는 user 모델과 연관된 country 의 code 컬럼을 같이 검색합니다.

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



Ref