Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Nirmalraj's avatar

Merging two queries, An end date less than current date with status confirmed(1) or cancelled(2) and also end date greater than equal to current date with status cancelled(2) only ? Solution please

Required reservation history o/p, An end date less than current date with status confirmed(1) or cancelled(2) and also end date greater than equal to current date with status cancelled(2) only.

$employeeId = $request->employee_id; $currentDate = Carbon::now()->toDateString();

    $reservationsHistory    = Booking::where(['user_id' => $employeeId])
        ->whereDate('end_date', '<', $currentDate)
        ->where(function ($query) {
            $query->Where('status', 1);
            $query->orWhere('status', 2);
        })
        ->orderByDesc('created_at')
        ->get();

    $reservationsHistory    = Booking::where(['user_id' => $employeeId])
        ->whereDate('end_date', '>=', $currentDate)
        ->Where('status', 2)
        ->orderByDesc('created_at')
        ->get();

    return $reservationsHistory;
0 likes
11 replies
jlrdw's avatar

Change the orWhere.

$query->Where('status', '<' 3);
Snapey's avatar

Isn't it just ALL records?

1 like
Nirmalraj's avatar

@Snapey Not all records. Records having end date less than current date with status confirmed(1) or cancelled(2) and also end date greater than equal to current date with status cancelled(2) only.

Snapey's avatar

@Nirmalraj

Records that are for the employee

that are before current date or after current date or equal to current date (can ignore this)

that are status 1 or status 2 (are there other status?)

ordered descending

is the same as;

$reservationsHistory    = Booking::where(['user_id' => $employeeId])
        ->whereIn('status',[1,2])
        ->orderByDesc('created_at')
        ->get();
2 likes
Nirmalraj's avatar

@Snapey Ok, but how to avoid upcoming reservation? Res history need to show upcoming reservation with cancelled also!

Snapey's avatar

@Nirmalraj You asked how to merge the two queries, but now you are adding additional requirements?

What status is 'cancelled' .... you see "magic numbers" are quite a code smell as they convey nothing

Snapey's avatar
$reservationsHistory    = Booking::where(['user_id' => $employeeId])
		->where(function($query) use($currentDate) {
				$query->where('end_date','<',$currentDate)->whereIn('status',[1,2]);
			})
       ->orWhere(function($query) use($currentDate) {
				$query->where('end_date','>=',$currentDate)->where('status',2]);
         }
        ->orderByDesc('created_at')
        ->get();
1 like
Nirmalraj's avatar

@Snapey Tq, but it also getting records from different employees because of the orWhere.

Snapey's avatar
Snapey
Best Answer
Level 122

$reservationsHistory    = Booking::query()
		->where(function($query) use($currentDate, $employeeId) {
				$query->where('end_date','<',$currentDate)
                       ->whereIn('status',[1,2])
                       ->where(['user_id' => $employeeId]);
			})
       ->orWhere(function($query) use($currentDate,$employeeId) {
				$query->where('end_date','>=',$currentDate)
                        ->where('status',2])
                        ->where(['user_id' => $employeeId]);
         }
        ->orderByDesc('created_at')
        ->get();
1 like

Please or to participate in this conversation.