Laravel Logic运算符中的查询

时间:2015-08-06 23:46:49

标签: php mysql laravel-5

我想查询Laravel DB,例如

SELECT * FROM `tickets` 
WHERE (created_at > '2015-07-01'
    AND created_at < '2015-07-31')
AND (state != 'Resolved'
    OR state != 'closed'
    OR state != 'Cancelled'
    OR state != 'Solution Rejected')
ORDER BY `id` DESC 

我尝试使用原始语句,但似乎没有用。因此,我尝试使用Laravel自己的模型函数存档相同的结果,但我遗漏了一些东西......这是我到目前为止所得到的。

Ticket::whereBetween('created_at', [$start, $end])
->whereIn('state',['Resolved','closed','Cancelled','Solution Rejected'])
->get();

PS:或者我有一种方法可以改为

SELECT * FROM `tickets` 
WHERE ( (created_at > '2015-07-01'
    AND created_at < '2015-07-31')
    OR (updated_at > '2015-07-01'
    AND updated_at < '2015-07-31') )
AND (state != 'Resolved'
    OR state != 'closed'
    OR state != 'Cancelled'
    OR state != 'Solution Rejected')
ORDER BY `id` DESC 

1 个答案:

答案 0 :(得分:1)

试试这个先生:

  DB::table('tickets')
        ->whereBetween('created_at',[$start, $end])
        ->orWhere(function($query)
        {
            $query->where('state', '!=', 'Resolved')
                  ->where('state', '!=', 'closed')
                  ->where('state', '!=', 'Cancelled')
                  ->where('state', '!=', 'Solution Rejected')
                  ->orderBy('id', 'desc');
        })
        ->get();