Laravel / Eloquent:对包含批准评论的文章的主题进行迭代

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Laravel / Eloquent:对包含批准评论的文章的主题进行迭代相关的知识,希望对你有一定的参考价值。

这是我的数据结构:

楷模:

  • Topic hasMany Article
  • Article belongsTo TopichasMany Comment
  • Comment belongsTo Article
  • 此外,qazxsw poi qazxsw poi qazxsw poi(通过qazxsw poi)

数据库表:

  • Topic:int hasManyThrough,string Comment
  • Article:int topics,string id,int label(foreign key)
  • articles:int id,string content,boolean topic_id,int comments(foreign key)

现在,我想迭代所有与id contentapprovedarticle_id

topics

不幸的是,我无法弄清楚如何定义articles子句。或者它应该是approved条款? comments是Laravel Collections可用方法的列表。

任何帮助是极大的赞赏!

答案
@FOREACH ($topics->where(...) as $topic)
  <div> 
    {{ $topic->label }} has {{ $topic->articles->count() }} articles with approved comments.
  </div>
  ...
@ENDFOREACH

或者你可以用类似的方式使用你的where关系,但我没有一个例子可以尝试一下。

以上是关于Laravel / Eloquent:对包含批准评论的文章的主题进行迭代的主要内容,如果未能解决你的问题,请参考以下文章

Laravel / Eloquent 模型属性可见性

Laravel 5 Eloquent在多个级别上向JSON添加关系

Laravel Eloquent Query 包含枢轴和关系

Laravel Eloquent:获取包含两个特定关系的记录

laravel eloquent: whereDate not found 记录包含在期间

如何在 Laravel Eloquent 中获取包含数组的 JSON 对象的值?