097. 将自然语句转换为数据库查询语句——lorisleiva/laravel-search-string
将自然语句转换为数据库查询语句 ——lorisleiva/laravel-search-string#
今天介绍的这个扩展包 —— github.com/lorisleiva/laravel-sear... ,可以使用简单且可自定义的语法,基于一个唯一字符串生成数据库查询,简单来说就是通过一个字符串,来完成复杂的搜索。这里有一篇介绍的文章 翻译:[扩展推荐] 将自然语句转换为 Laravel...。
这节课我们在一个 5.8 的项目中测试一下。
安装#
$ composer require lorisleiva/laravel-search-string
$ php artisan vendor:publish --tag=search-string
为了方便查看日志,再安装一个扩展包 overtrue/laravel-query-logger
,将 SQL 语句打印到日志中。
使用#
这里为了方便测试,准备了两个模型 Topic 和 User,并填充了一些数据。
使用 Trait#
首先需要给模型增加 Trait。
app/Topic.php
use Lorisleiva\LaravelSearchString\Concerns\SearchString;
class Topic extends Model
{
use SearchString;
protected $searchStringColumns = [
'title', 'is_active', 'created_at',
];
定义一个接口
routes/api.php
Route::get('topics', function(Request $request) {
return App\Topic::usingSearchString($request->search_string)->get();
});
基础搜索#
定义好以后就可以针对这些字段进行搜索了 title=foobar is_active=0 created_at > 2019-05-13
。
定义别名#
app/Topic.php
protected $searchStringColumns = [
'title' => '标题',
'is_active' => '/^active|激活$/',
'created_at' => 'created',
];
简单的定义一下别名,就可以使用别名进行搜索了 标题=foobar 激活=0 created > 2019-05-13
。
详细定义#
每一个字段还可以进行详细的配置
app/Topic.php
protected $searchStringColumns = [
'title' => [
'key' => '/^title|标题$/',
'searchable' => true
],
'body' => [
'key' => 'content',
'searchable' => true
],
'is_active' => [
'key' => '/^active|激活$/',
'boolean' => true,
],
'created_at' => [
'key' => '/^created|创建时间$/',
'date' => true,
]
];
例如使用如下搜索语句 标题=foobar 激活 创建时间 = today
。
因为定义了 title 和 content 是 可搜索的,所以可以直接输入想要的字符,例如 foo not 激活 创建时间 = today
。
特殊关键字#
尝试使用如下语句搜索 foo limit:1 from:1 sort=-created_at
,会搜索标题和内容中包含 foo,每次只取一条记录,偏移一条,按创建时间倒叙。
定义在配置文件中#
扩展包支持我们将模型的搜索定义在配置文件中。
config/search-string.php
.
.
.
App\Topic::class => [
'columns' => [
'title' => [
'key' => '/^title|标题$/',
'searchable' => true
],
'body' => [
'key' => 'content',
'searchable' => true
],
'is_active' => [
'key' => '/^active|激活$/',
'boolean' => true,
],
'created_at' => [
'key' => '/^created|创建时间$/',
'date' => true,
]
],
],
];
错误处理#
默认情况下, 如果搜索条件有误,则会显示所有的结果,扩展包还提供了两种选择,抛出异常,或者不显示结果。