Php 如何在laravel中转换where匹配查询?
我想将以下查询转换为Laravel ORMPhp 如何在laravel中转换where匹配查询?,php,laravel,Php,Laravel,我想将以下查询转换为Laravel ORM $sql = "SELECT * FROM snippets WHERE MATCH(snippets_name,seo_description,snippets_description,snippet_tags) AGAINST (' ".$query."' IN BOOLEAN MODE) LIMIT " . $start . "," . $limit; 我尝试了以下代码 但是没有得到预期的结果 $searchdata =
$sql = "SELECT * FROM snippets WHERE MATCH(snippets_name,seo_description,snippets_description,snippet_tags)
AGAINST (' ".$query."' IN BOOLEAN MODE) LIMIT " . $start . "," . $limit;
我尝试了以下代码
但是没有得到预期的结果
$searchdata = DB::table('snippets')
->selectRaw("*")
->whereRaw("MATCH(snippets_name,seo_description,snippets_description,snippet_tags) AGAINST (' ".$query."' IN BOOLEAN MODE)")
->paginate(20);
你到底得到了什么结果 我以前使用过Eloquent实现了全文搜索,建议将
MATCH
子句移动到SELECT中,并根据返回的分数排序
此外,paginate()
不能很好地处理原始查询部分,因此最好使用take()
(限制)和skip()
(偏移量)创建自己的分页
最后,您应该使用准备好的查询绑定,而不是将$query
变量添加到原始SQL中
这可能适合您:
\DB::table('snippets')
->select([
'*',
\DB::raw("MATCH(snippets_name, seo_description, snippets_description, snippet_tags) AGAINST (? IN BOOLEAN MODE) AS score"),
])
->take($limit)
->skip($start)
->havingRaw('score > 0')
->orderBy('score', 'DESC')
->setBindings([$query])
->get();
我加入了
havingRaw()
以排除任何分数为0的结果,这些结果可能是不相关的。您可以共享您的数据表结构吗?此查询非常有效-$sql=“SELECT*FROM snippets WHERE MATCH(snippets\u name,seo\u description,snippets\u description,snippet\u标记)与(“.”查询匹配。“'在布尔模式下)限制“$start.”、“$LIMIT;