jhormantasayco / laravel-searchzy
简单轻量级的 Eloquent 模型搜索
2.1.1
2021-02-16 06:08 UTC
Requires
- php: ^7.2|7.3|^8.0
- illuminate/support: ~6.0|~7.0|~8.0
Requires (Dev)
- orchestra/testbench: ~4.0
- phpstan/phpstan: ^0.12
- phpunit/phpunit: ~6.0|~7.0|~8.0|~9.0
- sebastian/phpcpd: ^4.1
- squizlabs/php_codesniffer: 3.*
README
该软件包允许您以简单和简单的方式搜索和过滤 Laravel 的 Eloquent Model 记录。兼容版本 6、7、8。
安装
您可以通过以下方式使用 composer 安装此软件包
composer require jhormantasayco/laravel-searchzy
在模型中使用
要添加 searchzy,您需要做以下操作
- 在您的模型中使用
Jhormantasayco\LaravelSearchzy\Searchzytrait。 - 通过属性或返回数组的函数,以关联数组的格式指定,其中指定了 searchzy 用于搜索和过滤记录的列。数组的键代表存储请求数据的变量名,值代表模型的列或关系。要关联模型的关系,请使用以下命名约定
(relation:column),如下例所示
use Jhormantasayco\LaravelSearchzy\Searchzy; class MyModel extends Model { use Searchzy; /** * The attributes that are searchable via property. * * @var array */ protected $searchable = [ 'nombre' => 'name', 'dni' => 'code', 'telefono' => 'phone', 'correo' => 'email', 'post' => 'posts:title', 'descripcion' => 'posts:description', ]; /** * The attributes that are searchable via method. * * @return array */ public function searchableInputs(){ return [ 'nombre' => 'name', 'dni' => 'code', 'phone' => 'phone', 'email' => 'email', 'post' => 'posts:title', 'descripcion' => 'posts:description', ]; } /** * The attributes that are filterable via property. * * @var array */ protected $filterable = [ 'rol_id' => 'role_id', ]; /** * The attributes that are filterable via method. * * @return array */ public function filterableInputs(){ return [ 'rol_id' => 'role_id', ]; } }
属性或方法?
您可以使用以下示例中的任何一种方式,无论是通过属性还是方法,都不会有问题。使用您最舒适的方式。
在控制器中使用
只需将 searchzy scope 添加到您的查询中,即可根据请求中发送的数据进行搜索和过滤记录。Searchzy 只处理与模型中关联数组描述的名称匹配的数据。
public function index(){ // Obtiene los inputs del request y sus respectivos valores. $params = Usuario::searchzyInputs(); // Implementación de searchzy en la consulta donde puedes seguir usando los demás métodos del Model con total normalidad. $oUsuarios = Usuario::withCount(['posts AS posts_count']) ->with(['posts']) ->searchzy() ->orderBy('name') ->paginate(); return view('welcome.index', compact('params', 'oUsuarios')); }
在视图中使用
searchzy 实现了一个默认的 keyword,即 word。配置位于 config/searchzy.php,您可以根据需要更改此值。
搜索字段的实现方式如下
<input type="text" name="{{ config('searchzy.keyword') }}" value="{{ ${config('searchzy.keyword')} }}" class="form-control" class="Buscar a un usuario por su nombre, dni, telefono, correo electrónico, titulo o descripción de sus posts">
如果您使用的是 laravelcollective/html 包,则实现方式如下
{!! Form::text(config('searchzy.keyword'), ${config('searchzy.keyword')}, [ 'class' => 'form-control', 'placeholder' => 'Buscar a un usuario por su nombre, dni, telefono, correo electrónico, titulo o descripción de sus posts' ]) !!}
有了这个,我们就可以开始使用 searchzy 搜索我们的记录了。
对于可筛选的字段,建议为数组中的每个元素实现一个选择框。
示例
{!! Form::select('rol_id', UtilEnum::$ARR_ROLES, $rol_id, [ 'class' => 'form-control', ]) !!}
请注意,选择框的名称必须与模型中定义的键相同。
我们实现了什么?
- 避免在项目中的控制器或模型中过度使用搜索查询(orWhere 或 whereHas)。
- 为列和关系提供别名,以便进行搜索和过滤记录,避免任何可能查看 URL 参数的人推断数据库的设计和建模。
- 通过使用软件包生成的子查询优化搜索查询。
要求
- PHP 7.2 或更高版本。
- Laravel 6.0 或更高版本。
演示
您可以在以下链接中查看软件包的演示
测试
./vendor/bin/phpunit ./vendor/bin/phpcpd src ./vendor/bin/phpcs src ./vendor/bin/phpcbf src ./vendor/bin/phpstan analyse src
变更日志
请参阅 CHANGELOG 了解最近更改的信息。
贡献
请参阅 CONTRIBUTING 了解详细信息。
安全
如果您发现任何安全相关的问题,请通过电子邮件 jhormantasayco@gmail.com 联系我们,而不是使用问题跟踪器。
致谢
许可协议
MIT 许可协议(MIT)。有关更多信息,请参阅许可文件。