怎么优雅的多字段搜索
$topic = QueryBuilder::for(Topic::class)
->allowedIncludes('category', 'user')
->allowedFilters([
'title',
'auto_title',
'body',
AllowedFilter::exact('category_id'),
AllowedFilter::scope('withOrder')->default('recentReplied'),
])
->paginate(10);
这样的话,就是如下的链接请求
topics?include=user&page=1&filter[category_id]=&filter[title]=关键词&filter[auto_title]=关键词&filter[body]=关键词
这样的搜索就是,要么3个字段都满足才会有结果,要么只能搜索单一字段,怎么优化一下可以3个字段同时搜索呢?
=================
比如像下面这样就可以搜索多字段:
topics?include=user&page=1&filter[category_id]=&filter[keyword]=关键词
本作品采用《CC 协议》,转载必须注明作者和本文链接
拆分一下, 有条件的搜索 $array=compact ('category_id','status','user_id'); // 生成之后需要过滤一下非空 -字段名和数据表中的字段要搜索的字段对应 模糊搜索 最后合并 $query->whereRaw ("(concat (' 字段 1',' 字段 2',' 字段 3') like '%" . $keywords . "%')")->where ($array);
不太理解需求
是这样的吗
推荐一个我比较喜欢用的检索扩展,采用查询作用域扩展的 github.com/Tucker-Eric/EloquentFil...
可以看下我之前写的,不知道是不是你要的