[英]Laravel query builder does not work as expected using orWhere conditions
I want to convert the sql query to laravel我想将sql查询转换为laravel
my sql query is working fine and show only active_state=1 record but laravel query qhows inactive rows also.what I did wrong我的 sql 查询工作正常,只显示 active_state=1 记录,但 laravel 查询 qhows 也是非活动行。我做错了什么
select a.id,a.assigned_user_name,a.meeting_start_date,a.call_start_date
from mgl_report_targets as a
left join dcalendardate b on b.datevalue=a.meeting_start_date
left join dcalendardate c on c.datevalue=a.call_start_date
where (b.datevalue > '2020-07-31' and b.datevalue < '2020-09-01')
or ( c.datevalue > '2020-07-31' and c.datevalue < '2020-09-01')
and a.active_status='1'
I have tried我试过了
$report_details=DB::table('mgl_report_targets');
$report_details=$report_details->leftjoin('dcalendardate as a','a.datevalue','=',DB::raw("mgl_report_targets.meeting_start_date::date"));
$report_details=$report_details->leftjoin('dcalendardate as b','b.datevalue','=',DB::raw("mgl_report_targets.call_start_date::date"));
$report_details=$report_details->where('a.datevalue','>','2020-07-31');
$report_details=$report_details->where('a.datevalue','<','2020-09-01');
$report_details=$report_details->orWhere('b.datevalue','>','2020-07-31');
$report_details=$report_details->orWhere('b.datevalue','<','2020-09-01');
$report_details=$report_details->where('mgl_report_targets.active_status','=','1');
$report_details=$report_details->select('mgl_report_targets.id','mgl_report_targets.assigned_user_name','mgl_report_targets.meeting_start_date','mgl_report_targets.call_start_date')
->get();
What you are missing is the ()
wrapping the or conditions.您缺少的是()
包装或条件。 By using orWhere
, eloquent will only create a OR
condition, without wrapping it into a parenthesis.通过使用orWhere
,eloquent 只会创建一个OR
条件,而不会将其包装在括号中。 You need to use a callback:您需要使用回调:
$report_details=DB::table('mgl_report_targets')
->leftjoin('dcalendardate as a','a.datevalue','=',DB::raw("mgl_report_targets.meeting_start_date::date"))
->leftjoin('dcalendardate as b','b.datevalue','=',DB::raw("mgl_report_targets.call_start_date::date"))
->where(function ($query) { // <-- here you create the first '()`
return $query->where('a.datevalue','>','2020-07-31')
->where('a.datevalue','<','2020-09-01');
})
->orWhere(function ($query) { // <-- here you create the second '()`
return $query->where('b.datevalue','>','2020-07-31')
->where('b.datevalue','<','2020-09-01');
})
->where('mgl_report_targets.active_status','=','1')
->select('mgl_report_targets.id', 'mgl_report_targets.assigned_user_name', 'mgl_report_targets.meeting_start_date', 'mgl_report_targets.call_start_date')
This will create the following query:这将创建以下查询:
select `mgl_report_targets`.`id`, `mgl_report_targets`.`assigned_user_name`, `mgl_report_targets`.`meeting_start_date`, `mgl_report_targets`.`call_start_date`
from `mgl_report_targets`
left join `dcalendardate` as `a` on `a`.`datevalue` = mgl_report_targets.meeting_start_date::date
left join `dcalendardate` as `b` on `b`.`datevalue` = mgl_report_targets.call_start_date::date
where (`a`.`datevalue` > ? and `a`.`datevalue` < ?)
or (`b`.`datevalue` > ? and `b`.`datevalue` < ?)
and `mgl_report_targets`.`active_status` = ?
It looks equivalent to your query to me它看起来相当于你对我的查询
To better debug your query, you can use the dd()
command to print the actual sql you are running:为了更好地调试您的查询,您可以使用dd()
命令打印您正在运行的实际 sql:
// Instead of
$report_details->get();
// use
$report_details->dd();
Reference: https://laravel.com/docs/5.8/queries#debugging参考: https : //laravel.com/docs/5.8/queries#debugging
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.