【问题标题】:where datediff < 15 from today in query builder laravelwhere datediff < 15 from today in query builder laravel
【发布时间】:2016-12-27 16:40:37
【问题描述】:

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

--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 已经在 mysql 中以 date 格式
我想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 Illuminate\Database\Query\Expression could not be converted to int

如何解决?

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

【问题讨论】:

    标签: laravel laravel-4 laravel-query-builder


    【解决方案1】:

    试试这个代码

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

    【讨论】:

    • 我收到此错误Object of class Illuminate\Database\Query\Builder could not be converted to string
    • 你把 get() 方法放在最后了吗。表::whereDate('exp_date', 'get();
    • 哎呀...忘了:)
    • hmm...我仍然得到远离 expDate 的数据....我的意思是说我的规则,如果产品会在 exp 日期少于现在 15 天后发出通知...但是当我尝试了 16 天或更长时间,但仍在查询中……我哪里错了?
    • 如果您只想要过去 15 天的记录,请将条件更改为 >=
    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 2016-04-10
      • 1970-01-01
      • 2020-07-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-22
      • 1970-01-01
      • 2021-03-17
      相关资源
      最近更新 更多