Php 拉雷维尔口若悬河得到关系计数
我使用的是Laravel 5.3 我有两张桌子:Php 拉雷维尔口若悬河得到关系计数,php,laravel,Php,Laravel,我使用的是Laravel 5.3 我有两张桌子: Articles --------- id cat_id title 及 我已在我的模型中定义了我的关系: // Article model public function category() { return $this->belongsTo(Category::class); } // Category model public function children() { return $this->has
Articles
---------
id
cat_id
title
及
我已在我的模型中定义了我的关系:
// Article model
public function category()
{
return $this->belongsTo(Category::class);
}
// Category model
public function children()
{
return $this->hasMany(Category::class, 'parent_id', 'id');
}
是否有一个简单的方法使用雄辩有一个列表与文章计数的类别。困难在于,我想将类别分组为id\u parent=0
,即,我只想显示包含子项目计数的父类别
我试过这样的方法:
$category = new \App\Models\Category();
$categoryTable = $category->getTable();
return $category->leftJoin('article', 'article.cat_id', '=', 'category.id')
->whereIn('article.cat_id', function($query)
{
$query->select('cat_id')
->from('categories')
->where('categories.parent_id', ???)
->orWhere($this->tableName .'.cat_id', $id);
})
->groupBy('cat_id');
但是我迷路了…在您的类别
模型中定义一个articles()
关系如下:
public function articles()
{
return $this->hasMany(Article::class, 'cat_id');
}
然后您可以按以下方式进行尝试:
Category::where('parent_id', 0)->withCount('articles')->get();
您可以使用
with count()
。它可从5.3版本获得
有关雄辩的更多信息,请访问:这应该可以:
$category
->where('categories.parent_id', 0)
->leftJoin('article', 'article.cat_id', '=', 'categories.id')
->select('categories.id', \DB::raw('COUNT(article.id)'))
->groupBy('categories.id')
->get();
上面的查询将获得类别ID和属于该类别的所有文章的计数
再次阅读您的问题和评论后,如果我理解正确,您希望获得属于这些类别的所有文章的计数(父类id=0)+属于子类别的文章计数(父类id=(某个类别的id))
现在我没有办法轻松地测试它,但我认为沿着这些思路应该可以做到这一点
$category
->where('categories.parent_id', 0)
->leftJoin('article', 'article.cat_id', '=', 'categories.id')
->leftJoin('categories as c2', 'c2.parent_id', '=', 'categories.id')
->leftJoin('article as a2', 'a2.cat_id', '=', 'c2.id')
->select('categories.id', \DB::raw('(COUNT(article.id)) + (COUNT(a2.id)) as count'))
->groupBy('categories.id')
->get();
beign说,我认为你最好在categories中有一个名为count的列,并在每次添加新文章时更新它。为了提高性能。您可以使用
hasManyThrough()
雄辩的方法获取所有儿童的文章,然后将文章数加在一起,形成一个漂亮的小getter。我将getter添加到模型上的$appends
数组中,以帮助在Tinker输出中演示它
class Category extends Model
{
protected $appends = [
'articleCount'
];
public function articles()
{
return $this->hasMany(Article::class);
}
public function children()
{
return $this->hasMany(Category::class, 'parent_id');
}
public function childrenArticles()
{
return $this->hasManyThrough(Article::class, Category::class, 'parent_id');
}
public function getArticleCountAttribute()
{
return $this->articles()->count() + $this->childrenArticles()->count();
}
}
以下是Tinker的输出:
Psy Shell v0.8.0 (PHP 7.0.6 — cli) by Justin Hileman
>>> $cat = App\Category::first();
=> App\Category {#677
id: "1",
name: "Cooking",
parent_id: null,
created_at: "2016-12-15 18:31:57",
updated_at: "2016-12-15 18:31:57",
}
>>> $cat->toArray();
=> [
"id" => 1,
"name" => "Cooking",
"parent_id" => null,
"created_at" => "2016-12-15 18:31:57",
"updated_at" => "2016-12-15 18:31:57",
"articleCount" => 79,
]
>>>
如果要将类别查询限制为具有包含文章的子项的类别查询,可以使用has()
方法:
Category::has('children.articles')->get();
以下是有关has()
方法的详细信息:
Category::has('children.articles')->get();
以及hasManyThrough()
方法:
Category::has('children.articles')->get();
我确信仍有人在经历这一过程,我可以通过以下方式解决它,假设我有一个代理模型和一个时间表模型,即一个代理可能有多个时间表:
class Schedule extends Model {
public function agent() {
return $this->belongsTo(Agent::class, 'agent_id');
}
}
class Agent extends Model {
public function user(){
return $this->belongsTo(User::class);
}
public function schedules(){
return $this->hasMany(Schedule::class);
}
}
有些代理可能不一定分配了计划,因此,在调用with()
方法之前,我过滤了这些计划,如下所示:
$agents = Agents::whereIn(
'id',
Schedule::distinct()->pluck('agent_id')
)->with('schedules')->get();
希望这有帮助 在表
类别中有多少层次结构?最多只有2层,不超过2层,但如何计算父类别中有子类别的文章数?是的,但我也需要子类别的计数。我的意思是,如果一篇文章属于儿童类,我需要数一数。啊,太好了!它起作用了,但我这里有两个关系。它与关系文章->类别一起工作,但我也需要包括孩子们。耶!谢谢在我们可以访问Entity ex属性中的这个值之后:$category->articles\u count
withCount几乎每次我尝试它时都是n+1-对于一个小的10行数据库来说很好,但只是将您的行数增加到1000,然后突然它会执行1000次查询来计算每个项目