Laravel search for records 7 days old only

╄→尐↘猪︶ㄣ 提交于 2019-12-22 10:34:56

问题


i am trying to pull records that are 7 days old only , not older or earlier. But its not working, i'm using Carbon.

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

回答1:


I have a solution, but its not using Carbon.

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



回答2:


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




回答3:


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');


来源:https://stackoverflow.com/questions/46731298/laravel-search-for-records-7-days-old-only

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!