在今天的查询构建器laravel中,datediff <15

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在今天的查询构建器laravel中,datediff <15相关的知识,希望对你有一定的参考价值。

我在我的桌面产品中有这样的字段

--exp_date--
2016-08-02
2016-08-28
2016-08-28
2016-08-23
2016-08-15
2016-08-05
2016-08-20

exp_date已经在mysqldate格式 我想select数据,从今天起,剩下的exp_date日期不到15

我已经尝试过这样的事了

$dn = date('Y-m-d');          //today date
$query = DB::table('product')->where(DB::raw('datediff(exp_date), $dn') < 15)->get();

但我得到这个错误 Object of class IlluminateDatabaseQueryExpression could not be converted to int

怎么解决?

PS: 我使用laravel 4.2并且更喜欢它是由查询构建器或原始查询

答案

试试这段代码

$expDate = Carbon::now()->subDays(15));
Table::whereDate('exp_date', '<',$expDate); 
另一答案

在Laravel Query中,日期差异适用于我。

DB::table('product')->whereRaw('DATEDIFF(exp_date,current_date) < 15')->get();

以上是关于在今天的查询构建器laravel中,datediff <15的主要内容,如果未能解决你的问题,请参考以下文章

在查询构建器 laravel 中投射

在 laravel 查询构建器中使用多个 where 子句

在查询中使用 AND 的 Laravel 查询构建器

在 Laravel get() 查询构建器中附加两个数组

在执行查询之前,如何从 Laravel 的查询构建器中获取原始查询字符串?

Laravel:查询构建器