Laravel 只搜索 7 天前的记录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46731298/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 16:48:25  来源:igfitidea点击:

Laravel search for records 7 days old only

phplaravellaravel-5php-carbon

提问by xTRIFAC70Rx

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

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

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

回答by Maraboc

You can use whereDatefor that :

你可以使用whereDate

->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

whereDate 方法可用于将列的值与日期进行比较

PS : Since Laravel 5.3

PS:从 Laravel 5.3 开始

回答by MatHatrik

I have a solution, but its not using Carbon.

我有一个解决方案,但它不使用碳。

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

回答by porosh09

To sum my last 7 days of records:

总结我过去 7 天的记录:

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