【问题标题】:Laravel 4.2 WhereBetween Not WorkingLaravel 4.2 不工作的地方
【发布时间】:2015-07-28 07:05:43
【问题描述】:

我有一个如下结构的表格,

entry_added_date 是存储日期等效字段,(2015-07-27)。

现在我想用这个字段来归档两个日期之间的条目。

我试过下面的代码,

    $start = Input::get('start');
    $end = Input::get('end');

 $arr = DB::table('otc_revenue_entries')
        ->whereBetween('entry_added_date',array($start,$end))
        ->get();
 return Response::json(['data',$arr]);

我的代码哪里错了?

【问题讨论】:

  • 执行->toSql() 并显示生成了哪个 SQL。也许这会有所帮助,因为我认为代码是正确的。也许你应该使用 int 值来使用 Laravel 的 whereBetween()
  • 为什么你不使用created_at 而是entry_added_date ?表中是否有“正确”entry_added_date(非空且与您的 $start,$end 匹配)的行?

标签: php laravel-4 eloquent where-clause


【解决方案1】:

检查下面的代码..

    $startdate = new DateTime($start);
    $start_date = $startdate->format("Y-m-d");
    $enddate = new DateTime($end);
    $end_date = $enddate->format("Y-m-d");

$arr = DB::table('otc_revenue_entries')
        ->leftJoin('otc_users','otc_users.user_id','=','otc_revenue_entries.entry_added_by')
        ->leftJoin('otc_branches','otc_branches.branch_id','=','otc_revenue_entries.entry_branch')
        ->whereBetween('entry_added_date',array($start_date,$end_date))
        ->get();

return Response::json(['data',$arr]);

【讨论】:

    【解决方案2】:

    确保您的 $start 和 $end 实际上与您的 entry_added_date 格式相同,因此比较可能会失败。

    【讨论】:

      猜你喜欢
      • 2014-10-30
      • 2017-08-09
      • 1970-01-01
      • 2014-06-09
      • 2016-04-11
      • 1970-01-01
      • 2016-07-25
      • 2019-08-07
      • 1970-01-01
      相关资源
      最近更新 更多