laravel eloquent 嵌套关系计数

laravel eloquent nested relationship count

我有 4 个数据库表:

广播员 |编号 | public_name | .... |

文章 |编号 |标题| .... |

评论 |编号 | user_id| article_id | .... |

用户 |编号 |登入| .... |

我想列出所有广播公司以及每一个(广播公司)的文章数量和所有文章(广播公司)的总评论。在我编写的代码下方,但它错过了相同的东西

Broadcaster::withCount(['articles'])
                             ->withCount(['articles'=> function($query){
                                return $query->withCount("comments");
                             }])

                             ->get();

我查看了 Whosebug 论坛,但没有得到我的问题的任何答案,请帮忙并谢谢

您可以在 Broadcasters class:

中声明关系 hasManyThrough
function comments()
{
    return $this->hasManyThrough('App\Comment', 'App\Articles');
}

所以你可以

Broadcaster::withCount(['articles','comments'])

Laravel Doc