简体   繁体   中英

Laravel query builder does not work as expected using orWhere conditions

I want to convert the sql query to 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

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. 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:

// Instead of
$report_details->get();

// use
$report_details->dd();

Reference: https://laravel.com/docs/5.8/queries#debugging

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM