我有这个功能:
public function indexo2returneditems(Request $request)
{
$per_page = $request->per_page;
$sortBy = $request->sort_by;
$orderBy = $request->order_by;
$dateFrom = $request->dateFrom;
$dateTo = $request->dateTo;
return response()->json([
'ok1' => O2order::where([['result', '=', 'Failed'], ['status', '=', 'Failed']])
->whereDate('finisheddate', '<=', $dateTo)
->whereDate('finisheddate', '>=', $dateFrom)
->paginate($per_page),
'ok2' => O2order::where('deviceexchangedto', '<>', '')
->whereDate('finisheddate', '<=', $dateTo)
->whereDate('finisheddate', '>=', $dateFrom)
->paginate($per_page),
'not_ok' => O2order::where('result', '=', 'Failed')
->where('status', '=', 'Failed')
->orWhere('deviceexchangedto', '<>', '')
->whereDate('datecreated', '<=', $dateTo)
->whereDate('datecreated', '>=', $dateFrom)
->paginate($per_page),
], 200);
}
Ok1和Ok2正常运行,但是当我将它们连接在一起时,则无法正常工作
我在做什么错,我该怎么办?
答案 0 :(得分:1)
您需要在此使用where函数
尝试
$not_ok = O2order::where(function($query){
$query->where([['result', '=', 'Failed'], ['status', '=', 'Failed']])
->orWhere('deviceexchangedto', '<>', '');
})
->whereDate('datecreated', '<=', $dateTo)
->whereDate('datecreated', '>=', $dateFrom)
->paginate($per_page);
因为当您使用 orWhere 时,它只是在查询中放置了或,因此您的查询变成了类似的内容
select * form table1 where a = 'condition 1' and b = '2' or b = '3' and date1 = date(now())
您想要的是
select * form table1 where a = 'condition 1' and (b = '2' or b = '3') and date1 = date(now())