【问题标题】:laravel date search filter cannot display data between dateslaravel 日期搜索过滤器无法显示日期之间的数据
【发布时间】:2018-11-19 03:32:54
【问题描述】:

当我在日期之间运行我的日期范围过滤器时,没有数据显示,但在我的数据库中我有这些数据。

注意: 如果你知道如何使用碳,请在下面说明,谢谢。

例如:从 1-5-2018 到 31-5-2018 什么都不会显示,请看下图

See Image

控制器:

 if ($request->has('from') && $request->has('to')) {
        $from = date('d-M-Y', strtotime($request->get('from')));
        $to = date('d-M-Y', strtotime($request->get('to')));
        $leads = Lead::whereBetween('created_at', [$from, $to])->paginate(5);
    }

【问题讨论】:

  • 确保在whereBetween() 中,较小的日期放在首位,而不是较大的日期
  • 是的,但数据仍然是空的..
  • 数据库和变量中的日期格式是否相同?
  • im 使用我的 sql 默认格式 yyyy-mm-dd
  • 在你的变量中?

标签: php laravel random laravel-5.5


【解决方案1】:

使用 toDateString 代替 strtotime。这将起作用。

 if ($request->has('from') && $request->has('to')) {
            $from = $request->get('from')->toDateString();
            $to = $request->get('to')->toDateString();
            $leads = Lead::whereBetween('created_at', [$from, $to])->paginate(5);
        }

Ps:现在无法打开 imgur。看不到您的日期类型,如果这不起作用并且找不到任何解决方案,请添加您的日期表格。

【讨论】:

  • 我遇到此错误“调用字符串上的成员函数 toDateString()”
  • 您在带有 die dump dd(date('d-M-Y', strtotime($request->get('from')))) 的 if 语句中看到的内容;
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-11-27
  • 1970-01-01
  • 2013-05-29
  • 2021-03-17
  • 2013-08-23
  • 2022-01-03
  • 1970-01-01
相关资源
最近更新 更多