从今天开始,查询构建器 laravel 中的 datediff < 15

Nev*_*ack 2 laravel laravel-4 laravel-query-builder

我的表产品中有这样的字段

--exp_date--
2016-08-02
2016-08-28
2016-08-28
2016-08-23
2016-08-15
2016-08-05
2016-08-20
Run Code Online (Sandbox Code Playgroud)

exp_date已经在datemysql中的格式
我想要select数据exp_date从今天起小于 15

我先试试这样的

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

但我收到这个错误
Object of class Illuminate\Database\Query\Expression could not be converted to int

如何解决?

ps:
我使用了laravel 4.2,并且更喜欢通过查询构建器或原始查询

Par*_*ban 6

试试这个代码

$expDate = Carbon::now()->subDays(15));
Table::whereDate('exp_date', '<',$expDate); 
Run Code Online (Sandbox Code Playgroud)

  • 你把 get() 方法放在最后了吗?表::whereDate('exp_date', '&lt;',$expDate)-&gt;get(); (2认同)

小智 6

日期差异在 Laravel 查询中对我有用。

DB::table('product')->whereRaw('DATEDIFF(exp_date,current_date) < 15')->get();
Run Code Online (Sandbox Code Playgroud)