【问题标题】:Get posts which has at least 2 comments on the last 48 hours获取在过去 48 小时内至少有 2 条评论的帖子
【发布时间】:2022-11-17 20:24:18
【问题描述】:

我正在尝试获取过去 48 小时内至少有 2 个 cmet 的所有帖子。我正在使用以下代码:

    $posts= Post::has( 'comments', '>', 1 )->whereHas( 'comments', function( $comments ) {
        return $comments->where( 'created_at', '>', Carbon::now()->subDays(2) );
    })->get()->toArray();
  1. 至少有 2 个 cmets 工作正常。
  2. 在过去 48 小时内不工作。

【问题讨论】:

    标签: php laravel


    【解决方案1】:
    $date = Carbon::now()->subDays(2);
    
    $posts= Post::has( 'comments', '>', 1 )->whereHas( 'comments', function( $comments ){
            return $comments->where('created_at', '>=', $date);
        })->get()->toArray();
    

    你能这样试试吗?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-02-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-15
      • 1970-01-01
      • 2016-04-14
      相关资源
      最近更新 更多