承接 titasgailius/search-relations 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

titasgailius/search-relations

最新稳定版本:2.0.1

Composer 安装命令:

composer require titasgailius/search-relations

包简介

A Laravel Nova tool.

README 文档

README

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 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

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

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

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

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.

<?php

namespace Titasgailius\SearchRelations\Contracts;

use Illuminate\Database\Eloquent\Builder;

interface Search
{
    /**
     * Apply search for the given relation.
     *
     * @param  \Illuminate\Database\Eloquent\Builder  $query
     * @param  string  $relation
     * @param  string  $search
     * @return \Illuminate\Database\Eloquent\Builder
     */
    public function apply(Builder $query, string $relation, string $search): Builder;
}

You may take a look at the Titasgailius\SearchRelations\Searches\RelationSearch class as an example.

统计信息

  • 总下载量: 6.9M
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 361
  • 点击次数: 1
  • 依赖项目数: 6
  • 推荐数: 0

GitHub 信息

  • Stars: 359
  • Watchers: 4
  • Forks: 39
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-01-04