【问题标题】:Using withCount and Distinct with Laravel在 Laravel 中使用 withCount 和 Distinct
【发布时间】:2019-08-06 17:49:48
【问题描述】:

我想为此模型创建一个 withCount 子查询。

感谢Iqbal Butt 我有这个 sn-p 用于计数。

$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 = actions.set_idaction_type_id = 1 和 set_id = 1)


按 cmets 编辑


文章模型

<?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
name

article_set

id
set_id
article_id

【问题讨论】:

  • 如果没有有关数据库结构的更多详细信息,很难知道您要做什么。您可以发布所涉及表的迁移/模型吗?此外,您可能对 laravel.com/docs/5.8/queries#conditional-clauses 感兴趣,以便有条件地将子句添加到您的 eloquent 查询中。
  • @Peter 添加了更多信息,谢谢
  • 好的,谢谢。您能否提供更多关于您想要通过查询获得什么的详细信息?给定集合的文章计数?抱歉,我不清楚您需要什么。
  • 当然,我已经编辑了我的问题。当用新的眼光审视我的问题时,action_id 非常混乱,所以我将其更改为 action_type_id。再次感谢。

标签: laravel eloquent


【解决方案1】:

尽管它使用返回的集合来计算计数,但它可以获取您需要的数据。

$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;

【讨论】:

    猜你喜欢
    • 2019-11-16
    • 1970-01-01
    • 2019-09-23
    • 2020-01-30
    • 2020-05-29
    • 1970-01-01
    • 1970-01-01
    • 2013-04-24
    • 2021-03-20
    相关资源
    最近更新 更多