【问题标题】:Laravel eloquent get model on dateLaravel eloquent 获取模型日期
【发布时间】:2014-10-07 08:34:18
【问题描述】:

我想获取在指定日期创建的所有用户:

// $date is a Carbon instance parsed from command line argument.
// I checked it and it is correct.

$users = User::where('created_at', '>', $date->startOfDay())
    ->where('created_at', '<', $date->endOfDay())
    ->get();

但这会返回 0 个结果,而在数据库中有对应于该日期的行。

我做错了什么?

【问题讨论】:

    标签: php datetime laravel eloquent php-carbon


    【解决方案1】:

    Carbon 的行为不像值对象(即它不是不可变的),因此:

    $date->startOfDay();
    $date->endOfDay();
    

    只需修改$date 对象并将其返回。话虽如此,传递给查询的字符串是在 PDO 在准备好的语句中绑定它时获得的,此时 $date 已经突变为 endOfDay

    这意味着你只是传递对对象的引用:

    $start === $end; // true
    

    所以要么使用不同的对象:

    $users = User::where('created_at', '>', $date->copy()->startOfDay())
      ->where('created_at', '<', $date->copy()->endOfDay())
      ->get();
    

    或者直接返回你需要的字符串,而不是 Carbon 对象:

    $users = User::where('created_at', '>', $date->startOfDay()->toDateTimeString())
      ->where('created_at', '<', $date->endOfDay()->toDateTimeString())
      ->get();
    

    不过,$date 现在将保留 xxxx-xx-xx 23:59:59 时间戳,因此请记住这一点,以防您需要在其他地方使用此变量。

    【讨论】:

      【解决方案2】:

      这里的问题不是 Laravel 本身,而是 Carbon。

      使用以下代码时:

      use Carbon\Carbon;
      $date = new Carbon('2014-10-07');
      
      $start = $date->startOfDay();
      
      $end = $date->endOfDay();
      
      echo $start.' '.$end;
      

      你得到的是:

      2014-10-07 23:59:59 2014-10-07 23:59:59
      

      所以 Laravel 会执行查询:

      select * from `users` where `created_at` >'2014-10-07 23:59:59' and `created_at` <'2014-10-07 23:59:59';
      

      显然你不会得到任何结果。

      如您所见,$start 结果不是您所期望的。

      为了使它起作用,我发现的解决方案是创建 2 个碳对象:

      use Carbon\Carbon;
      $date = new Carbon('2014-10-07');
      $date2 = new Carbon('2014-10-07');
      
      $start = $date->startOfDay();
      
      $end = $date2->endOfDay();
      
      echo $start.' '.$end;
      

      现在结果如预期:

      2014-10-07 00:00:00 2014-10-07 23:59:59
      

      现在你可以使用了:

      use Carbon\Carbon;
      $date = new Carbon('2014-10-07');
      $date2 = new Carbon('2014-10-07');
      
      $users = User::where('created_at', '>', $date->startOfDay())
          ->where('created_at', '<', $date2->endOfDay())
          ->get();
      

      use Carbon\Carbon;
      $date = new Carbon('2014-10-07');
      $date2 = new Carbon('2014-10-07');
      
      $users = User::whereBetween('created_at', [$date->startOfDay(), $date2->endOfDay()])->get();
      

      【讨论】:

        【解决方案3】:

        在您的查询中留下表名:

        $users = DB::table('users')
                        ->where('votes', '>', 100)
                        ->orWhere('name', 'John')
                        ->get();
        

        看:http://laravel.com/docs/4.2/queries#selects

        【讨论】:

          猜你喜欢
          • 2017-01-12
          • 2015-09-05
          • 2021-02-10
          • 1970-01-01
          • 2021-11-19
          • 2020-08-04
          • 1970-01-01
          • 2019-10-04
          • 2018-12-05
          相关资源
          最近更新 更多