Laravel 仅搜索 7 天前的记录

Laravel search for records 7 days old only

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

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

你可以使用 whereDate :

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

documentation中:

The whereDate method may be used to compare a column's value against a date

PS : 因为 Laravel 5.3

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

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

总结一下我最近 7 天的记录:

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