Laravel或哪里 [英] Laravel or where
本文介绍了Laravel或哪里的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
目前,我正在Laravel上的一个项目上工作,但我遇到了麻烦.我想创建一个如下的SQL语句:
Currently I am working on a project in Laravel but I am stuck.I want to create a SQL statement like this:
SELECT * FROM SPITems WHERE publisher_id=? AND feed_id=? AND (title LIKE '%?%' OR description LIKE '%?%')
现在我有了以下代码:
$query = SPItem::orderBy('title');
if(isset($_GET['publisherID']) && is_numeric($_GET['publisherID']))
{
$query = $query->where('publisher_id', $_GET['publisherID']);
}
if(isset($_GET['productFeedID']) && is_numeric($_GET['productFeedID']))
{
$query = $query->where('program_id', $_GET['feedID']);
}
if(isset($_GET['search']))
{
$query = $query->orWhere('title', 'like', '%' . $_GET['search'] . '%');
$query = $query->where('description', 'like', '%' . $_GET['search'] . '%');
}
但这会生成:
SELECT * FROM SPITems WHERE (publisher_id=? AND feed_id=?) OR (title LIKE '%?%') AND description LIKE '%?%'
如何获得正确的或"订单?
How can I get the correct "or" order?
推荐答案
查看文档中的参数分组部分:
https://laravel.com/docs/master/queries#parameter-grouping
它说明了如何在WHERE子句中对条件进行分组.
It explains how to group conditions in the WHERE clause.
应该是这样的:
if(isset($_GET['search']))
{
$query = $query->where(function($query){
$query->where('title', 'like', '%' . $_GET['search'] . '%')
->orWhere('description', 'like', '%' . $_GET['search'] . '%');
});
}
这篇关于Laravel或哪里的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文