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

问题描述 投票:0回答:1

这是我的数据结构:

楷模:

  • 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可用方法的列表。

任何帮助是极大的赞赏!

php database laravel eloquent
1个回答
1
投票
@FOREACH ($topics->where(...) as $topic)
  <div> 
    {{ $topic->label }} has {{ $topic->articles->count() }} articles with approved comments.
  </div>
  ...
@ENDFOREACH

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

© www.soinside.com 2019 - 2024. All rights reserved.