:::

11-1 讀出時加入各種條件

  1. 修改/專案/app/Http/Controllers/ExamController.php
    public function index()
    {
        $exams = Exam::where('enable', 1)
            ->orderBy('created_at', 'desc')
            ->take(10)
            ->get();
        return view('exam.index', compact('exams'));
    }
  2. where()用來指定條件,我們指定有啟用的測驗

  3. orderby()用來指定排序,我們指定用建立時間做反向排序(由新到舊)

  4. take()用來抓出數量

  5. get()取得資料

  6. 若要用兩個(and)篩選條件:

    $exams = Exam::where('enable', 1)
        ->where('created_at', '>', '2018-05-30')
  7. 若要用 or,則用閉包+orWhere()方式:

    $exams = Exam::where(function ($query) {
        $query->where('enable', 1)
            ->orWhere('user_id', 1);
    })
  8. 詳情可參考:https://laravel-china.org/docs/laravel/5.6/queries/1398#387ca8

到GitHub觀看此單元程式異動


:::

書籍目錄

展開 | 闔起

http%3A%2F%2Fcampus-xoops.tn.edu.tw%2Fmodules%2Ftad_book3%2Fpage.php%3Ftbsn%3D37%26tbdsn%3D1173

計數器

今天: 2400240024002400
昨天: 1988198819881988
總計: 7389291738929173892917389291738929173892917389291