Home > Back-end >  How to check if predefined selected column is null is laravel
How to check if predefined selected column is null is laravel

Time:06-11

I have this code :

$employee_calendar->attendance = Attendance::leftJoin("attendance_corrections", function ($join) {
                $join->on("attendance_corrections.attendance_id", "=", "attendance.id")
                    ->where("attendance_corrections.status", "!=", "rejected");
            })
            ->select('attendance.*', DB::raw('IF(`time` IS NOT NULL, `time`, attendance_corrections.correct_time) as `correctTime`'))
            ->where("attendance.employee_id", $employee->id)
            ->whereDate("attendance.date", "=", $employee_calendar->date)
            ->orderBy('correctTime', 'asc')
            ->with("requests")
            ->distinct()
            ->get();

I want to check if correctTime is null then dont show this data. I have tried ->whereNotNull('correctTime') it is giving error say column not found.

CodePudding user response:

Since its a derived column you have to use having clause, the best thing of the raw query is that you can use multiple types of operators such as AND, OR etc just by passing as a string.

->havingRaw("correctTime IS NOT NULL")

CodePudding user response:

Use it like

->whereNotNull('attendance_corrections.correct_time')
  • Related