[英]How to check if predefined selected column is null is laravel
我有這個代碼:
$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();
我想檢查 correctTime 是否為空,然后不顯示此數據。 我試過 ->whereNotNull('correctTime') 它給出了錯誤說找不到列。
像這樣使用它
->whereNotNull('attendance_corrections.correct_time')
由於它是一個派生列,因此您必須使用 having 子句,原始查詢的最佳之處在於您可以使用多種類型的運算符,例如 AND、OR 等,只需將其作為字符串傳遞即可。
->havingRaw("correctTime IS NOT NULL")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.