johnylemon / laravel-searchable
轻松搜索和过滤用户模型
Requires
- johnylemon/miscellaneous: ^0.1.0
- laravel/framework: ^7.0 || ^8.0
Requires (Dev)
- orchestra/testbench: ^6.2
- phpunit/phpunit: ^9.4
README
这个库使得搜索和过滤变得非常简单。简单就是魔法。
入门
- 添加仓库
composer require johnylemon/laravel-searchable
-
如果自动注册失败,请注册
Johnylemon\Searchable\Providers\SearchableServiceProvider提供者。 -
使用发布命令发布配置
php artisan vendor:publish
- 将
Johnylemon\Searchable\Traits\Searchable特性添加到任何你想要的模型中,并定义包含可搜索属性的searchable方法
<?php use Johnylemon\Searchable\Traits\Searchable; class User extends Model { use Searchable; /** * Searchable properties array * * @return array searchables */ protected function searchable(): array { return [ 'name', 'email', ]; } }
- 享受吧!
基本用法
要启用当前查询的搜索,请使用 withSearch 范围
ModelName::withSearch()->get();
下面的示例将返回所有名为 John 的用户
Route::get('/users?name=John', function(){ return ModelName::withSearch()->get(); });
这将返回将 name 设置为 John 并将 nick 设置为 johnylemon 的用户
GET /users?name=John&nick=johnylemon
有时你可能想要传递多个可能的值。要实现这一点,请为给定属性使用值数组。
此示例将返回所有将 name 设置为 johny 或 lemon 的用户,并将 nick 设置为 johnylemon。每个可能的 name 值都将使用为 name 属性定义的过滤器。
GET /users?name[]=john&name[]=lemon&nick=johnylemon
当然,你可以为多个属性传递数组。
此示例将返回所有将 name 设置为 johny 或 lemon 的用户,并将 nick 设置为 johnylemon 或 laravel。
GET /users?name[]=john&name[]=lemon&nick[]=johnylemon&nick[]=laravel
默认搜索过滤器
默认情况下,每个属性都将使用 Johnylemon\Searchable\Search\BasicSearch 搜索过滤器进行过滤。此过滤器将使用简单的 where("property", "value") 条件。您可以在配置文件中随意更改此设置。
此软件包附带了一些实用的搜索过滤器,可用于常见搜索
Like 搜索过滤器
使用 Johnylemon\Searchable\Search\LikeSeach 类。将添加 %LIKE% 条件。
LikeBegin 搜索过滤器
使用 Johnylemon\Searchable\Search\LikeBeginSeach 类。将添加 LIKE% 条件。
LikeEnd 搜索过滤器
使用 Johnylemon\Searchable\Search\LikeEndSeach 类。将添加 %LIKE 条件。
请随意使用它们进行搜索。
自定义可搜索项
可搜索数组可能返回比简单属性名称更复杂的数组。
您可以定义每个字段应使用哪个过滤器
use App\Search\CustomSearch; /** * Searchable properties array * * @return array searchables */ public function searchable(): array { return [ // this field will be searched using `BasicSearch` class 'last_name', // will use `CustomSearch` filter for `first_name` field 'first_name' => CustomSearch::class, // instead of using custom class, you may use anonymous function 'full_name' => function($query, $property, $value) { $query->where(DB::raw("first_name || ' ' || last_name"), $value); }, // `name` field will be searched same way as `first_name` // so it will use `CustomSearch` class 'name' => 'first_name', ]; }
注意,full_name 字段在数据库中不存在。
别名
有时在搜索查询中隐藏真实列名称可能很有用。这就是别名发挥作用的地方。上面的示例使用了两个在数据库中不存在的字段:full_name 和 name。
full_name 属性使用匿名函数,该函数将使用简单的连接来搜索给定值。
name 属性指向 first_name 属性,因此它将被视为 first_name 字段。当然,first_name 必须是可搜索的。
如果您希望使用 name 字段作为 first_name 字段,但又不希望使 first_name 可搜索,请使用自定义搜索类或匿名函数
use App\Search\SearchAsFirstName; use App\Search\SearchAsAnotherField; public function searchable(): array { return [ // // use callable // 'name' => function($query, $property, $value) { $query->where('first_name', $value); }, // // or custom class // 'name' => SearchAsFirstName::class, // // or even search class instance // 'name' => new SearchAsAnotherField('first_name'), ]; }
这个简单的示例将搜索 ghost 用户,归功于别名
public function searchable(): array { return [ 'ghost' => function($query, $property, $value) { $query->whereNotNull('deleted_at'); }, ]; }
缩写
当然,每次都输入整个类名或调用可能很麻烦,因此此软件包允许您在配置文件中定义自定义的、易于输入和记忆的缩写。
'shorthands' => [ 'ghost' => App\Search\GhostUsers::class, 'awesomness' => App\Search\AwesomeSearchFilter::class, ],
缩写优先于字段名。假设您有一个 name 字段,还有一个指向 name 字段的 username 字段,同时还有一个 name 缩写。在这种情况下,将使用缩写而不是 name 字段。
// // config file // `shorthands` => [ 'name' => App\Search\NameSearch::class, ], // // model searchable method // public function searchable(): array { return [ 'name', 'username' => 'name', // `name` will be treated as shorthand, so `App\Search\NameSearch` search filter will be used ]; }
内置的搜索过滤器也可以用作 like、like-begin 和 like-end 缩写。
public function searchable(): array { return [ // will be searched using `%LIKE%` provided by `LikeSearch` class 'email' => 'like', // will be searched using `LIKE%` provided by `LikeBeginSearch` class 'code' => 'like-begin', // will be searched using `%LIKE` provided by `LikeEndSearch` class 'suffix' => 'like-end', ]; }
命令
此软件包附带 searchable:generate 命令,可用于快速生成自定义过滤器类。
此命令将在配置文件中指定的目录中放置全新的类。
php artisan searchable:generate MySearch
测试
您可以使用以下命令运行测试
vendor/bin/phpunit
许可
MIT 许可证 (MIT)
联系
请访问我的网站 https://johnylemon.dev
由 johnylemon 热情开发。