<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之旅 廣告
                ### [](https://octobercms.com/docs/database/relations#querying-relations)查詢關系 由于可以通過函數調用所有類型的Model關系,因此您可以調用這些函數來獲取關系的實例,而無需實際執行關系查詢。此外,所有類型的關系還可以用作[查詢構建器](https://octobercms.com/docs/database/query),使您可以繼續將約束鏈鏈接到關系查詢上,最后對數據庫執行SQL。 例如,想象一個博客系統,其中一個`User`模型具有許多關聯的`Post`模型: ~~~ class User extends Model { public $hasMany = [ 'posts' => ['Acme\Blog\Models\Post'] ]; } ~~~ ### [](https://octobercms.com/docs/database/relations#querying-method)通過關系方法訪問 您可以使用方法查詢**職位**關系,并向關系中添加其他約束`posts`。這使您能夠在關系上鏈接任何[查詢構建器](https://octobercms.com/docs/database/query)方法。 ~~~ $user = User::find(1); $posts = $user->posts()->where('is_active', 1)->get(); $post = $user->posts()->first(); ~~~ ### [](https://octobercms.com/docs/database/relations#querying-dynamic-property)通過動態屬性訪問 如果不需要向關系查詢添加其他約束,則可以簡單地訪問該關系,就好像它是一個屬性一樣。例如,繼續使用我們的`User`和`Post`示例模型,我們可以`$user->posts`改為使用屬性訪問用戶的所有帖子。 ~~~ $user = User::find(1); foreach ($user->posts as $post) { // ... } ~~~ 動態屬性是“延遲加載”,這意味著它們僅在您實際訪問它們時才加載其關系數據。因此,開發人員經常使用[急切的加載](https://octobercms.com/docs/database/relations#eager-loading)來預加載他們知道在加載模型后將對其進行訪問的關系。預先加載大大減少了必須執行的SQL查詢才能加載模型的關系。 ### [](https://octobercms.com/docs/database/relations#querying-existence)查詢關系存在 訪問模型的記錄時,您可能希望根據關系的存在來限制結果。例如,假設您要檢索所有具有至少一條評論的博客文章。為此,您可以將關系的名稱傳遞給`has`方法: ~~~ // Retrieve all posts that have at least one comment... $posts = Post::has('comments')->get(); ~~~ 您還可以指定一個運算符并計數以進一步自定義查詢: ~~~ // Retrieve all posts that have three or more comments... $posts = Post::has('comments', '>=', 3)->get(); ~~~ 嵌套`has`語句也可以使用“點”符號構造。例如,您可以檢索所有具有至少一項評論和投票的帖子: ~~~ // Retrieve all posts that have at least one comment with votes... $posts = Post::has('comments.votes')->get(); ~~~ 如果您需要更大的功能,可以使用`whereHas`和`orWhereHas`方法在`has`查詢中放置“ where”條件。這些方法使您可以向關系約束中添加自定義約束,例如檢查注釋的內容: ~~~ // Retrieve all posts with at least one comment containing words like foo% $posts = Post::whereHas('comments', function ($query) { $query->where('content', 'like', 'foo%'); })->get(); ~~~
                  <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>

                              哎呀哎呀视频在线观看