这是我的数据结构:
型号:
Topic hasMany ArticleArticle belongsTo Topic和hasMany CommentComment belongsTo ArticleTopic hasManyThrough Comment (通过Article)DB表:
topics:int id,string labelarticles:int id,string content,int topic_id(外键)comments:int id,string content,boolean approved,int article_id(外键)现在,我想迭代所有有topics和approved comments的articles。
@FOREACH ($topics->where(...) as $topic)
<div>
{{ $topic->label }} has {{ $topic->articles->count() }} articles with approved comments.
</div>
...
@ENDFOREACH不幸的是,我不知道如何定义where-clause。或者应该是when-clause?这里是用于Laravel的方法列表。
任何帮助都是非常感谢的!
发布于 2018-04-20 07:38:04
$topics = Topic::whereHas('articles', function ($query) {
$query->whereHas('comments', function ($query) {
$query->where('approved', true);
});
})->get();
@foreach ($topics as $topic)
<div>
{{ $topic->label }} has {{ $topic->articles->count() }} articles with approved comments.
</div>
@endforeach或者,您也可以以类似的方式使用您的hasManyThrough关系,但是我没有一个示例来尝试它。
https://stackoverflow.com/questions/49935729
复制相似问题