:::
11-1 讀出時加入各種條件
- 修改
/專案/app/Http/Controllers/ExamController.phppublic function index() { $exams = Exam::where('enable', 1) ->orderBy('created_at', 'desc') ->take(10) ->get(); return view('exam.index', compact('exams')); } -
where()用來指定條件,我們指定有啟用的測驗 -
orderby()用來指定排序,我們指定用建立時間做反向排序(由新到舊) -
take()用來抓出數量 -
get()取得資料

-
若要用兩個(and)篩選條件:
$exams = Exam::where('enable', 1) ->where('created_at', '>', '2018-05-30') -
若要用 or,則用閉包+orWhere()方式:
$exams = Exam::where(function ($query) { $query->where('enable', 1) ->orWhere('user_id', 1); }) -
詳情可參考:https://laravel-china.org/docs/laravel/5.6/queries/1398#387ca8
11. 讀出所有測驗
