Laravel search for records 7 days old only

柔情痞子 提交于 2019-12-06 13:53:16

I have a solution, but its not using Carbon.

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

You can use whereDate for that :

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

In the documentation :

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

PS : Since Laravel 5.3

porosh09

To sum my last 7 days of records:

$date = \Carbon\Carbon::today()->subDays(7);
$Profitinsevendays = DB::table('n_profit_loss')->where('datetime', '>=', $date)->sum('profit_or_loss');
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!