【问题标题】:Getting the total count of replies for all topics in a forum获取论坛中所有主题的回复总数
【发布时间】:2019-06-23 15:32:58
【问题描述】:

数据库是这样设置的:

  • 论坛($id)
  • 主题($id,$forum)
  • 帖子($id,$topic)

通过执行以下命令可以轻松获取主题总数:

$total_topics = DB::table("topics")->where("forum", "=", $forum->id)->get()

但是,我现在正在努力获取论坛中帖子的总数。我认为加入是要走的路,但我正在努力让任何事情发挥作用。 加入方式是否正确?

【问题讨论】:

    标签: mysql database laravel join eloquent


    【解决方案1】:

    是的 - 你需要在这里加入:

    $postsCount = DB::table('topics')
      ->join('posts', 'posts.topic', '=', 'topics.id')
      ->where('forum', $forum->id)
      ->count();
    

    【讨论】:

      猜你喜欢
      • 2016-09-02
      • 1970-01-01
      • 2010-11-05
      • 1970-01-01
      • 1970-01-01
      • 2011-07-12
      • 2012-10-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多