'Laravel Eloquent when date condition

I have an eloquent query, and inside it I have 2 condition. The first is when attendance_time == today and second is attendance_time == yesterday. I want to use that condition but I don't know how.

I've tried like code bellow, but still not working.

Attendance::where('employee_id', $request->employee_id)
      ->where('in_out', 'in')
      ->when('attendance_time' == Carbon::today(), function ($q) {
            return $q->where('attendance_time', Carbon::today());
      })->when('attendance_time' == Carbon::yesterday(), function ($q) {                  
            return $q->where('attendance_time', Carbon::yesterday());
      })->first();

Format for attendance_time is Y-m-d H:i:s



Solution 1:[1]

You can use whereBetween method:

   use Carbon/Carbon;

   ...

   Attendance::where('employee_id', $request->employee_id)
      ->where('in_out', 'in')
      ->whereBetween('attendance_time', [
          Carbon::now()->subDay()->toDateString(),
          Carbon::now()->toDateString()
      ])->first();

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Daniel Mesa