代码之家  ›  专栏  ›  技术社区  ›  whoacowboy

与count一起使用,与laravel一起使用

  •  0
  • whoacowboy  · 技术社区  · 6 年前

    我想为这个模型创建一个WithCount子查询。

    多亏了 Iqbal Butt 我有这段话要数。

    $count = Action::select('article_id as assigned');
    
    if (!empty($action_type_id))
    {
        $count = $count->where('action_type_id', $action_type_id);
    }
    
    if (!empty($set_id))
    {
        $count = $count->where('set_id', $set_id);
    }
    
    $count = $count->distinct('article_id')->count('article_id');
    

    我想这样做,但我觉得这是痛苦的缺陷。

    澄清编辑


    我有一个集到文章的多对多关系。

    每一篇文章都有许多动作。

    动作可以有多种动作类型。

    我需要计算给定集合中每个项目的操作类型。


    $sets = Set::withCount(['actions' => function ($q) use ($action_type_id, $set_id) {
    
        $q->select('article_id as assigned');
    
        if (!empty($action_type_id))
        {
            $q->where('action_type_id', $action_type_id);
        }
    
        if (!empty($set_id))
        {
            $q->where('set_id', $set_id);
        }
    
        $q->distinct('article_id')
          ->count('article_id'); 
        // I believe this is executing inner query
    }])
    
    ->get();
    
    return $sets;   
    

    这给了我一个错误,更可能是因为内部查询正在执行,而没有外部查询。

    sqlstate[42s22]:未找到列:1054中的未知列“sets.id” “where子句”(sql:select count(distinct article_id )作为骨料 从 actions 哪里 sets . id = 行动 . set_id action_type_id =1和 塞特伊德 = 1)


    按评论编辑


    物品模型

    <?php
    
    namespace App;
    
    use Illuminate\Database\Eloquent\Model;
    
    class Article extends Model
    {
        /**
         * Get the sets for the article.
         */
        public function sets()
        {
            return $this->belongsToMany(Set::class);
        }
    
        public function actions()
        {
            return $this->hasMany(Action::class);
        }
    }
    

    集合模型

    <?php
    
    namespace App;
    
    use Illuminate\Database\Eloquent\Model;
    
    class Set extends Model
    {
        /**
         * Get the articles for the set.
         */
        public function articles()
        {
            return $this->belongsToMany(Article::class);
        }
    
        public function actions()
        {
            return $this->hasMany(Action::class);
        }
    }
    

    行动模式

    <?php
    
    namespace App;
    
    use Illuminate\Database\Eloquent\Model;
    
    class Action extends Model
    {
        /**
         * Get the set that owns the action.
         */
        public function set()
        {
            return $this->belongsTo(Set::class);
        }
        /**
         * Get the article that owns the action.
         */
        public function article()
        {
            return $this->belongsTo(Article::class);
        }
    }
    

    数据库

    行动

    id
    set_id
    article_id
    action_type_id 
    

    集合

    id
    name
    

    物品

    身份证件
    名称
    

    文章集

    id
    set_id
    article_id
    
    0 回复  |  直到 6 年前
        1
  •  1
  •   Peter 4lbertyson    6 年前

    这里有一些东西可以得到您需要的数据,尽管它使用返回的集合来计算计数。

    $articles = Set::find($set_id)
        ->articles()
        ->with('actions')
        ->get()
        ->map(function($article){
            $article->action_type_count = $article->actions->unique('action_type')->count();
            return $article;
        });
    

    这将给你一系列的文章。操作类型计数在属性中 action_type_count 收藏中的每一篇文章。因此要获取第一篇文章的操作类型计数:

    $articles->first()->action_type_count;