Skip to content
Advertisement

How to check if predefined selected column is null is laravel

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.

Advertisement

Answer

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")
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement