luchmewep / granular-search
该包已废弃,不再维护。未建议替代包。
细粒度搜索和聚合提供了四个(4)可用的特质,这些特质对于搜索和聚合非常有用。
3.0
2021-04-26 17:35 UTC
Requires
- illuminate/support: ~7|~8
Requires (Dev)
- orchestra/testbench: ~5|~6
- phpunit/phpunit: ~9.0
README
细粒度搜索是Eloquent模型的一个实用工具,用于实现更少的代码行数进行搜索、过滤和排序。
安装
通过Composer
$ composer require luchmewep/granular-search
使用方法
要使用此包,您有两种选择。您可以在Eloquent模型上实现它,或者只需使用此包的主要方法getGranularSearch
(请参阅GranularSearchTrait
)。
然而,要充分利用此包,最佳方法是实现准备好的特质和抽象模型。如果您仍然不确定,您可以仅使用非模型侵入方式,即使用GranularSearch
类本身。
在Eloquent模型上实现
在Eloquent模型上实现细粒度搜索有两种方法
- 通过扩展
AbstractGranularModel
或AbstractGranularUserModel
(强烈建议)用于基于用户的模型。
- 通过在Eloquent模型上使用
GranularSearchableTrait
。
扩展抽象模型
目前有两个(2)模型可供选择。对于与身份验证无关的模型,您可以直接使用AbstractGranularModel
。对于与身份验证相关的模型,建议使用AbstractGranularUserModel
。
这两个抽象类并不难理解,因为它们只是使用了GranularSearchTrait
和GranularSearchableTrait
。它们只是事先准备好以最小化特质相关的错误。
一旦扩展,您应设置以下受保护的静态字段。
// Model fields to be excluded from filtering protected static $granular_excluded_keys = []; // Model fields to use LIKE instead instead of equal protected static $granular_like_keys = []; // Relations to consider for filtering/searching protected static $granular_allowed_relations = []; // Relations to include on 'q' search (must be a subset of allowed relations array) protected static $granular_q_relations = [];
示例
- 常规搜索/过滤
- 使用模型自己的字段进行过滤
- 使用
q
作为参数进行过滤
- 关系型搜索/过滤
- 通过
relations
使用其他模型的前缀字段进行过滤
- 通过
relations
使用q
参数进行过滤
手动实现
如果您目前不打算在当前的Eloquent模型上尝试,您可能希望首先使用GranularSearch
类本身进行测试。由于getGranularSearch是静态方法,我们不需要实例化GranularSearch类。以下是静态方法的参数
/**
* Filter the model collection using the contents of the $request variable.
*
* @param Request|array $request Contains all the information regarding the HTTP request
* @param Model|Builder $model Model or query builder that will be subjected to searching/filtering
* @param string $table_name Database table name associated with the $model
* @param array $excluded_keys Request keys or table column names to be excluded from $request
* @param array $like_keys Request keys or table column names to be search with LIKE
* @param string $prepend_key
* @param bool $ignore_q
* @return Model|Builder
*/
示例
- 使用模型自己的字段进行过滤
- 使用
q
作为参数进行过滤
变更日志
有关最近更改的更多信息,请参阅变更日志。
测试
$ composer test
贡献
请参阅 contributing.md 以获取详细信息及待办事项列表。
安全性
如果您发现任何与安全性相关的问题,请通过电子邮件联系作者,而不是使用问题跟踪器。
致谢
许可证
许可证。请参阅许可证文件以获取更多信息。