【问题标题】:Laravel between dates日期之间的 Laravel
【发布时间】:2017-11-17 20:49:52
【问题描述】:

我想通过在 eloquent 中比较日期来检查促销代码的有效性,但不知道为什么它不起作用

$promoCode = self::where('code', $code)
        ->where('start_date', '>=', Carbon::now()->toDateString())
        ->where('end_date', '<=', Carbon::now()->toDateString())
        ->first();

如果我注释掉第 2 行和第 3 行,那么它工作正常。 MySQL 列类型是 date。我也尝试了 whereDate 方法,但那也不起作用

【问题讨论】:

    标签: laravel eloquent


    【解决方案1】:

    看起来你倒置了标志。您正在寻找早于start_date 且低于end_date 的东西。

    【讨论】:

      【解决方案2】:

      start_date 不应该早于现在。如果是现在应该在之间。试试:

      $promoCode = self::where('code', $code)
          ->where('start_date', '<=', Carbon::now()->toDateString())
          ->where('end_date', '<=', Carbon::now()->toDateString())
          ->first();
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-05-06
        • 1970-01-01
        • 2014-09-09
        • 1970-01-01
        • 2016-08-15
        • 1970-01-01
        • 2021-11-28
        相关资源
        最近更新 更多