【问题标题】:Laravel search for records 7 days old onlyLaravel 仅搜索 7 天前的记录
【发布时间】:2018-03-25 16:07:08
【问题描述】:

我正在尝试提取仅 7 天前的记录,而不是更早或更早的记录。 但它不起作用,我正在使用 Carbon。

->where(DB::raw('date(AppDate)'), Carbon::now()->subDays(7))

【问题讨论】:

    标签: php laravel laravel-5 php-carbon


    【解决方案1】:

    总结我最近 7 天的记录:

    $date = \Carbon\Carbon::today()->subDays(7);
    $Profitinsevendays = DB::table('n_profit_loss')->where('datetime', '>=', $date)->sum('profit_or_loss');

    【讨论】:

      【解决方案2】:

      我有一个解决方案,但它没有使用 Carbon。

       ->whereRaw('DATE(AppDate) = DATE_SUB(CURDATE(), INTERVAL 7 DAY)')
      

      【讨论】:

        【解决方案3】:

        您可以为此使用whereDate

        ->whereDate('created_at', Carbon::now()->subDays(7))
        ->get();
        

        documentation

        whereDate 方法可用于将列的值与 日期

        PS : 从 Laravel 5.3 开始

        【讨论】:

        • @Marabox 我忘了说我使用的是 Laravel 5.0
        • 没关系,它会帮助其他女巫使用 laravel 5.3 或更新版本;)
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-07-11
        • 2022-07-20
        • 1970-01-01
        相关资源
        最近更新 更多