【发布时间】: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_id和action_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。再次感谢。