• <small id='MfYkJ'></small><noframes id='MfYkJ'>

  • <i id='MfYkJ'><tr id='MfYkJ'><dt id='MfYkJ'><q id='MfYkJ'><span id='MfYkJ'><b id='MfYkJ'><form id='MfYkJ'><ins id='MfYkJ'></ins><ul id='MfYkJ'></ul><sub id='MfYkJ'></sub></form><legend id='MfYkJ'></legend><bdo id='MfYkJ'><pre id='MfYkJ'><center id='MfYkJ'></center></pre></bdo></b><th id='MfYkJ'></th></span></q></dt></tr></i><div id='MfYkJ'><tfoot id='MfYkJ'></tfoot><dl id='MfYkJ'><fieldset id='MfYkJ'></fieldset></dl></div>
  • <tfoot id='MfYkJ'></tfoot><legend id='MfYkJ'><style id='MfYkJ'><dir id='MfYkJ'><q id='MfYkJ'></q></dir></style></legend>

      • <bdo id='MfYkJ'></bdo><ul id='MfYkJ'></ul>
      1. Laravel/Eloquent : hasManyThrough WHERE

        时间:2023-09-23
          <tbody id='e0VUl'></tbody>

        • <bdo id='e0VUl'></bdo><ul id='e0VUl'></ul>
        • <small id='e0VUl'></small><noframes id='e0VUl'>

              <i id='e0VUl'><tr id='e0VUl'><dt id='e0VUl'><q id='e0VUl'><span id='e0VUl'><b id='e0VUl'><form id='e0VUl'><ins id='e0VUl'></ins><ul id='e0VUl'></ul><sub id='e0VUl'></sub></form><legend id='e0VUl'></legend><bdo id='e0VUl'><pre id='e0VUl'><center id='e0VUl'></center></pre></bdo></b><th id='e0VUl'></th></span></q></dt></tr></i><div id='e0VUl'><tfoot id='e0VUl'></tfoot><dl id='e0VUl'><fieldset id='e0VUl'></fieldset></dl></div>
            1. <tfoot id='e0VUl'></tfoot>
              • <legend id='e0VUl'><style id='e0VUl'><dir id='e0VUl'><q id='e0VUl'></q></dir></style></legend>
                1. 本文介绍了Laravel/Eloquent : hasManyThrough WHERE的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  在 Eloquent 的文档中,据说我可以将所需关系的键传递给 hasManyThrough.

                  In the documentation of Eloquent it is said that I can pass the keys of a desired relationship to hasManyThrough.

                  假设我有名为 Country、User、Post 的模型.通过用户模型,国家模型可能有许多帖子.也就是说,我可以直接调用:

                  Lets say I have Models named Country, User, Post. A Country model might have many Posts through a Users model. That said I simply could call:

                  $this->hasManyThrough('Post', 'User', 'country_id', 'user_id');
                  

                  到目前为止一切正常!但是我如何才能只为 id 为 3 的用户获取这些帖子?

                  有人可以帮忙吗?

                  推荐答案

                  就这样:

                  models: Country 有很多 User 有很多 Post

                  models: Country has many User has many Post

                  这允许我们在您的问题中使用 hasManyThrough :

                  This allows us to use hasManyThrough like in your question:

                  // Country model
                  public function posts()
                  {
                    return $this->hasManyThrough('Post', 'User', 'country_id', 'user_id');
                  }
                  

                  您想获取此关系的给定用户的帖子,因此:

                  You want to get posts of a given user for this relation, so:

                  $country = Country::first();
                  $country->load(['posts' => function ($q) {
                    $q->where('user_id', '=', 3);
                  }]);
                  // or
                  $country->load(['posts' => function ($q) {
                    $q->has('user', function ($q) {
                      $q->where('users.id', '=', 3);
                    });
                  })
                  
                  $country->posts; // collection of posts related to user with id 3
                  

                  <小时>

                  但是如果你改用它,它会更容易、更易读和更有说服力:(因为当您在寻找 id 为 3 的用户的帖子时,它与国家无关)


                  BUT it will be easier, more readable and more eloquent if you use this instead: (since it has nothing to do with country when you are looking for the posts of user with id 3)

                  // User model
                  public function posts()
                  {
                    return $this->hasMany('Post');
                  }
                  
                  // then
                  $user = User::find(3);
                  // lazy load
                  $user->load('posts');
                  // or use dynamic property
                  $user->posts; // it will load the posts automatically
                  // or eager load
                  $user = User::with('posts')->find(3);
                  
                  $user->posts; // collection of posts for given user
                  

                  总结一下:hasManyThrough是一种直接获取嵌套关系的方法,即.给定国家/地区的所有帖子,而不是搜索特定的 through 模型.

                  To sum up: hasManyThrough is a way to get nested relation directly, ie. all the posts for given country, but rather not to search for specific through model.

                  这篇关于Laravel/Eloquent : hasManyThrough WHERE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:如何自定义 Laravel 的 DatabaseQueryBuilder(制作更好的 下一篇:laravel 4:验证唯一(数据库)多个 where 子句

                  相关文章

                  最新文章

                  <legend id='bqCk7'><style id='bqCk7'><dir id='bqCk7'><q id='bqCk7'></q></dir></style></legend>
                  <i id='bqCk7'><tr id='bqCk7'><dt id='bqCk7'><q id='bqCk7'><span id='bqCk7'><b id='bqCk7'><form id='bqCk7'><ins id='bqCk7'></ins><ul id='bqCk7'></ul><sub id='bqCk7'></sub></form><legend id='bqCk7'></legend><bdo id='bqCk7'><pre id='bqCk7'><center id='bqCk7'></center></pre></bdo></b><th id='bqCk7'></th></span></q></dt></tr></i><div id='bqCk7'><tfoot id='bqCk7'></tfoot><dl id='bqCk7'><fieldset id='bqCk7'></fieldset></dl></div>

                  <small id='bqCk7'></small><noframes id='bqCk7'>

                  <tfoot id='bqCk7'></tfoot>

                      • <bdo id='bqCk7'></bdo><ul id='bqCk7'></ul>