<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                預載入是用來減少 N + 1 查詢問題。例如,一個 Book 模型數據會關聯到一個 Author 。關聯會像下面這樣定義: ~~~ class Book extends Model { public function author() { return $this->belongsTo('App\Author'); } } ~~~ 現在考慮下面的代碼: ~~~ foreach (Book::all() as $book) { echo $book->author->name; } ~~~ 上面的循環會執行一次查詢取回所有數據庫表上的書籍,然而每本書籍都會執行一次查詢取得作者。所以若我們有 25 本書,就會進行 26次查詢。 很幸運地,我們可以使用預載入大量減少查詢次數。使用 with 方法指定想要預載入的關聯對象: ~~~ foreach (Book::with('author')->get() as $book) { echo $book->author->name; } ~~~ 現在,上面的循環總共只會執行兩次查詢: ~~~ select * from books select * from authors where id in (1, 2, 3, 4, 5, ...) ~~~ 使用預載入可以大大提高程序的性能。 當然,也可以同時載入多種關聯: ~~~ $books = Book::with('author', 'publisher')->get(); ~~~ 甚至可以預載入巢狀關聯: ~~~ $books = Book::with('author.contacts')->get(); ~~~ 上面的例子中, author 關聯會被預載入, author 的 contacts 關聯也會被預載入。 預載入條件限制 有時您可能想要預載入關聯,同時也想要指定載入時的查詢限制。下面有一個例子: ~~~ $users = User::with(['posts' => function($query) { $query->where('title', 'like', '%first%'); }])->get(); ~~~ 上面的例子里,我們預載入了 user 的 posts 關聯,并限制條件為 post 的 title 字段需包含 "first" 。 當然,預載入的閉合函數里不一定只能加上條件限制,也可以加上排序: ~~~ $users = User::with(['posts' => function($query) { $query->orderBy('created_at', 'desc'); }])->get(); ~~~ ## 延遲預載入 也可以直接從模型的 collection 預載入關聯對象。這對于需要根據情況決定是否載入關聯對象時,或是跟緩存一起使用時很有用。 ~~~ $books = Book::all(); $books->load('author', 'publisher'); ~~~ 你可以傳入一個閉包來對查詢構建器進行條件限制: ~~~ $books->load(['author' => function($query) { $query->orderBy('published_date', 'asc'); }]); ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看