【问题标题】:Order by count in many to many polymorphic relation in LaravelLaravel中多对多多态关系中的按计数排序
【发布时间】:2019-01-22 07:29:21
【问题描述】:

让我们以文档中的示例为例:https://laravel.com/docs/5.7/eloquent-relationships#many-to-many-polymorphic-relations 很容易得到所有posts 和他们的tags 计数做Post::withCount('tags')->get()。

但是如何获取所有 tags 的使用次数?让它们按最常用/较少使用的顺序排列。

如果我这样做Tag::withCount(['video', 'post'])->get(),我将有两个属性videos_count 和posts_count。就我而言,我想要一个独特的taggables_count,它将是两者的总和。在完美的世界中,通过添加查询数据透视表的子选择。

【问题讨论】:

    标签: laravel laravel-5 eloquent


    【解决方案1】:

    我建议直接调用你已经做过的电话,即Tag::withCount(['video', 'post'])->get(),并将其添加到你的标签模型中:

    // Tag.php
    class Tag
    {
      ...
      // Create an attribute that can be called using 'taggables_count'
      public function getTaggablesCountAttribute()
      {
        return $this->videos_count + $this->posts_count;
      }
      ...
    }
    

    然后在你的循环中(或者你使用集合中的项目):

    @foreach($tags as $tag)
      {{ $tag->taggables_count }}
    @endforeach
    

    此设置要求您获取带有withCount['video', 'post'] 的标签。如果不这样做,您可能会得到0以换取$tag->taggables_count。

    如果您真的关心速度,则必须手动创建查询并在其中进行添加。

    【讨论】:

      【解决方案2】:

      所以经过更多搜索后,我发现仅在一个查询中无法做到这一点,因为在mysql 中我们无法对子选择结果进行选择。因此,执行Tag::withCount(['videos', 'posts']) 并尝试在查询中求和videos_count 和posts_count 将不起作用。我最好的方法是创建一个在数据透视表中读取结果的范围:

      public function scopeWithTaggablesCount($query) {
          if (is_null($query->getQuery()->columns)) {
              $query->select($query->getQuery()->from . '.*');
          }
      
          $query->selectSub(function ($query) {
              $query->selectRaw('count(*)')
                  ->from('taggables')
                  ->whereColumn('taggables.tag_id', 'tags.id');
          }, 'taggables_count');
      
          return $query;
      }
      

      使用它:

      $tags = Tag::withTaggablesCount()->orderBy('name', 'ASC')->get();
      

      所以现在每个标签都有一个taggables_count,它可以用于order by。希望它可以帮助其他人。

      【讨论】:

        猜你喜欢
        • 2014-08-25
        • 2014-05-17
        • 1970-01-01
        • 1970-01-01
        • 2021-12-27
        • 2023-01-23
        • 2011-08-23
        • 1970-01-01
        • 2017-02-28
        相关资源
        最近更新 更多