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

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

        <legend id='rbZeq'><style id='rbZeq'><dir id='rbZeq'><q id='rbZeq'></q></dir></style></legend>
        • <bdo id='rbZeq'></bdo><ul id='rbZeq'></ul>
      1. Laravel eloquent 获取所有记录,其中包含多对多关系

        时间:2023-09-24
          <i id='gz7Oa'><tr id='gz7Oa'><dt id='gz7Oa'><q id='gz7Oa'><span id='gz7Oa'><b id='gz7Oa'><form id='gz7Oa'><ins id='gz7Oa'></ins><ul id='gz7Oa'></ul><sub id='gz7Oa'></sub></form><legend id='gz7Oa'></legend><bdo id='gz7Oa'><pre id='gz7Oa'><center id='gz7Oa'></center></pre></bdo></b><th id='gz7Oa'></th></span></q></dt></tr></i><div id='gz7Oa'><tfoot id='gz7Oa'></tfoot><dl id='gz7Oa'><fieldset id='gz7Oa'></fieldset></dl></div>

                <bdo id='gz7Oa'></bdo><ul id='gz7Oa'></ul>
              • <legend id='gz7Oa'><style id='gz7Oa'><dir id='gz7Oa'><q id='gz7Oa'></q></dir></style></legend><tfoot id='gz7Oa'></tfoot>

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

                    <tbody id='gz7Oa'></tbody>

                  本文介绍了Laravel eloquent 获取所有记录,其中包含多对多关系中的所有 id的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我有一个 Posts 表,它有三个字段 idtitledescription.

                  I have a Posts table it has three fields id, title, description.

                  我的发布模型

                  class Post extends Model
                  {
                      use SoftDeletes;
                  
                      protected $fillable = ['title', 'description'];
                  
                      public function tags()
                      {
                          return $this->belongsToMany(Tag::class, 'post_tag');
                      }
                  }
                  

                  我的标签模型

                  class Tag extends Model
                  {
                      use SoftDeletes;
                  
                      protected $fillable = ['name'];
                  
                      public function posts()
                      {
                          return $this->belongsToMany(Post::class, 'post_tag');
                      }
                  }
                  

                  现在我想获得帖子&在我有标签过滤器的地方分页,例如我有两个标签 animals &news id 1 &2.现在我想获取所有带有标签 1 & 的帖子2 &分页.这是我试过的

                  Now I want to get posts & paginate where I have a tag filter e.g I have two tags animals & news which has id 1 & 2. Now I want to get all posts which has tag 1 & 2 & paginate. Here is what I tried

                          Post:: with('tags')->whereHas('tags', function($q) {
                              $q->whereIn('id', [1, 2]);
                          })->paginate();
                  

                  但在这里,我是 whereIn,它返回的帖子有标签 12both.但我想要同时具有标签 ID 1 和标签的帖子2.

                  But here as I am whereIn it returns posts has tags 1 or 2 or both. But I want post who has both tag id 1 & 2.

                  我使用的是 Laravel 5.2.

                  推荐答案

                  我一直在寻找相同的东西并受到 this stackoverflow MySQL answer,我已经结束了这个

                  I have been looking for the same thing and inspired by this stackoverflow MySQL answer, I have ended up with this

                  代码:

                  Post:: with('tags')->whereHas('tags', function($q) {
                      $idList = [1,2];
                      $q->whereIn('id', $idList)
                        ->havingRaw('COUNT(id) = ?', [count($idList)])
                  })->paginate();
                  

                  因为我想我可能会在一些地方使用它,所以我已经把它变成了一个特性,你可以在这里查看.如果您在 Post 类中包含 trait,您可以像下面这样使用.

                  Because I think I might use it in a few places I have made it into a trait which you can view here. Which if you included the trait in your Post class you could use like the following.

                  代码:

                  Post::with('tags')->whereHasRelationIds('tags', [1,2])->paginate();
                  

                  这篇关于Laravel eloquent 获取所有记录,其中包含多对多关系中的所有 id的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:在 Eloquent 中计算两条记录之间的值差异 下一篇:Laravel - 根据动态参数扩展 Eloquent where 子句

                  相关文章

                  最新文章

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

                    1. <tfoot id='yDePX'></tfoot>
                    2. <i id='yDePX'><tr id='yDePX'><dt id='yDePX'><q id='yDePX'><span id='yDePX'><b id='yDePX'><form id='yDePX'><ins id='yDePX'></ins><ul id='yDePX'></ul><sub id='yDePX'></sub></form><legend id='yDePX'></legend><bdo id='yDePX'><pre id='yDePX'><center id='yDePX'></center></pre></bdo></b><th id='yDePX'></th></span></q></dt></tr></i><div id='yDePX'><tfoot id='yDePX'></tfoot><dl id='yDePX'><fieldset id='yDePX'></fieldset></dl></div>

                      <legend id='yDePX'><style id='yDePX'><dir id='yDePX'><q id='yDePX'></q></dir></style></legend>

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