akki-io / laravel-nova-search
扩展 Laravel Nova 搜索功能的包
Requires
- php: ^7.3|^8.0
Requires (Dev)
- orchestra/testbench: ^5.4
- phpunit/phpunit: ^9.0
README
Laravel Nova Search
测试状态
此包提供了一个特质,用于扩展 Laravel Nova 资源的行为。
安装
您可以通过 composer 安装此包
composer require akki-io/laravel-nova-search
接下来,将 AkkiIo\LaravelNovaSearch\LaravelNovaSearchable
特质添加到您的基资源 App\Nova\Resource
类中。
use AkkiIo\LaravelNovaSearch\LaravelNovaSearchable; abstract class Resource extends NovaResource { use LaravelNovaSearchable; // ... }
用法
此包没有模糊匹配功能。如果您需要由“真实”搜索引擎提供的强大模糊匹配功能,请查看 Laravel Scout。
此包为您添加以下类型的搜索到 Laravel Nova 资源。
- 使用连接符搜索多个列。
- 搜索列中的每个单词。
- 搜索关系列。
使用连接符搜索多个列。
要定义哪些资源字段可搜索,您可以在资源类的 public static $searchConcatenation
属性中分配一个二维数组,包含数据库列。数组中的每个数组都是使用空格连接的列名。
/** * The columns that should be concatenated and searched. * * @var array */ public static $searchConcatenation = [ ['first_name', 'last_name'], ['first_name', 'company'], ];
搜索列中的每个单词。
要定义哪些资源字段可搜索,您可以在资源类的 public static $searchMatchingAny
属性中分配一个包含数据库列的数组。您的输入中的每个单词都在这些列中搜索。
/** * The columns that should be searched for any matching entry. * * @var array */ public static $searchMatchingAny = [ 'first_name', 'last_name', 'email', ];
搜索关系列。
要定义哪些资源字段可搜索,您可以在资源类的 public static $searchRelations
属性中分配一个包含数据库列的数组。这些数据库列用于搜索的相关表。此数组以关系名称为键,以要搜索的列的数组为值。
/** * The relationship columns that should be searched. * * @var array */ public static $searchRelations = [ 'posts' => ['title', 'sub_title'], ];
嵌套关系
您可以使用点符号搜索嵌套关系。
/** * The relationship columns that should be searched. * * @var array */ public static $searchRelations = [ 'user.location' => ['state_abbr', 'country_abbr'], ];
使用连接符搜索关系中的多个列。
要定义哪些资源字段可搜索,您可以在资源类的 public static $searchRelationsConcatenation
属性中分配一个二维数组,包含数据库列。数组中的每个数组都是使用空格连接的列名。
/** * The relationship columns that should to be concatenated and searched. * * @var array */ public static $searchRelationsConcatenation = [ 'user' => [ ['first_name', 'last_name'], ['email'] ], ];
在关系列中搜索每个单词。
为了定义哪些资源字段是可搜索的,您可以在您的资源类的public static $searchRelationsMatchingAny
属性中分配一个数据库列数组。您的输入中的每个单词都会在这些所有列中进行搜索。
/** * The relationship columns that should be searched for any matching entry. * * @var array */ public static $searchRelationsMatchingAny = [ 'user' => ['first_name', 'last_name'], ];
测试
composer test
贡献
请参阅贡献指南获取详细信息。
安全性
如果您发现任何与安全相关的问题,请通过电子邮件hello@akki.io联系,而不是使用问题跟踪器。
鸣谢
许可
MIT 许可证(MIT)。请参阅许可文件获取更多信息。