小编gia*_*our的帖子

使用Laravel的查询构建器查询日期列

使用Laravel查询构建器针对具有日期列的表编写查询时,应使用哪种数据类型?以下返回13个结果,但不返回任何内容:

$date = new \DateTime("-2 days");
Model::whereDate($date)->get();
Run Code Online (Sandbox Code Playgroud)

转储查询显示laravel正在尝试此操作:

array(3) {
  'query' =>
  string(62) "select * from `table` where `date` = ?"
  'bindings' =>
  array(1) {
    [0] =>
    class DateTime#310 (3) {
      public $date =>
      string(19) "2013-10-07 14:39:11"
      public $timezone_type =>
      int(3)
      public $timezone =>
      string(3) "UTC"
    }
  }
  'time' =>
  double(5.55)
}
Run Code Online (Sandbox Code Playgroud)

编写查询Model::whereDate($date->format("Y-m-d"))->get()相反有效,但似乎不像Laravel那样做.

是否有特定的对象类型我应该传递给查询以按日期列过滤?

编辑:此示例中的模型如下所示:

class Model extends Eloquent {
    protected $table = 'table';

    public function getDates() {
        return array('date');
    }
}
Run Code Online (Sandbox Code Playgroud)

它引用的表看起来像这样:

CREATE TABLE …
Run Code Online (Sandbox Code Playgroud)

php laravel eloquent laravel-4

2
推荐指数
1
解决办法
2万
查看次数

标签 统计

eloquent ×1

laravel ×1

laravel-4 ×1

php ×1