我有一個日曆(FullCalendar),用戶可以根據幾個參數(Tutor Secondary Tutor,Lesson,Location)篩選結果。當用戶對查詢進行更改時,它會碰到以下代碼。Laravel:基於輸入查詢。如果輸入爲空,請獲取全部
我遇到的問題是'OR'。我真正想要的是一個IF輸入爲空然後全部獲得。
如果用戶{獲取所有課程其中lead_tutor_id = 1和secondary_tutors_id = 1} 如果用戶和地點{獲取教訓,其中用戶是如上述,但具有LOCATION_ID = 3} 等,等
所以,如果只設置了一個或兩個過濾器,是否有一種方法可以回退以獲得所有結果?
$current_events = Calendar::Where(function($query) use ($start_time, $end_time, $tutor, $location, $lesson)
{
$query->whereBetween('date_from', [$start_time, $end_time])->orderBy('date_from')
->whereRaw('lead_tutor_id = ?
OR secondary_tutors_id = ?
OR location_id = ?
OR lesson_id = ?',
[
$tutor, // Input get() for user
$tutor, // Input get() for user
$location, // Input get() for location
$lesson, // Input get() for lesson
]
);
})->with('lessons', 'leadtutor', 'secondarytutor')->get();
我一直在玩查詢範圍,但這似乎失敗,如果傳遞一個NULL值通過它。
任何幫助,非常感謝。提前致謝。
1.你不需要在第一封'where',2。你不需要'whereRaw'可言, 3.你可能需要關閉所有的「或」輪,4.究竟是什麼問題?查詢範圍不會因爲NULL而失敗,因此請重新考慮您的問題。 – 2014-11-24 16:04:39