使用Laravel的查询生成器查询日期列。

时间:2021-12-30 15:30:05

When writing a query against a table with a date column using the Laravel query builder, what data type should be used? The following should return 13 results but instead returns nothing:

当使用Laravel查询生成器对具有日期列的表编写查询时,应该使用什么数据类型?下面应该返回13个结果,但不返回任何结果:

$date = new \DateTime("-2 days");
Model::whereDate($date)->get();

Dumping the query shows that laravel is trying this:

倾销这个查询表明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)
}

Writing the query as Model::whereDate($date->format("Y-m-d"))->get() instead works but doesn't seem like the Laravel way to do it.

将查询写入Model::whereDate($date->格式(“Y-m-d”))—>get(),相反,它可以工作,但似乎不是最简单的方法。

Is there a specific object type I should be passing into the query to filter by a date column?

是否存在一个特定的对象类型,我应该将其传递到查询中以通过日期列进行筛选?

EDIT: The model in this example looks like this:

编辑:本例中的模型如下:

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

    public function getDates() {
        return array('date');
    }
}

The table it refers to looks like this:

它所指的表格是这样的:

CREATE TABLE table(
  some_field VARCHAR(255),
  date DATE
) ENGINE = 'InnoDB';

Laravel thinks the date column is a string: Model::lists('date') returns an array of strings, as does DB::table('table')->lists('date').

Laravel认为date列是一个字符串:Model: list ('date')返回一个字符串数组,DB:::table('table')->list ('date')也返回一个字符串数组。

Changing $date = new \DateTime("-2 days"); to $date = \Carbon\Carbon::now()->subDays(2) doesn't allow me to query the date column directly with the Carbon object.

$date = new \DateTime("-2天");$date = \Carbon\Carbon: now()->subDays(2)不允许我直接使用Carbon object查询date列。

UPDATED: For whatever reason, the built-in MySQL date to \Carbon\Carbon isn't working for me, so the simplest solution was to write one as a query scope:

更新:无论出于什么原因,内置的MySQL日期都不能为我工作,所以最简单的解决方案是将其中一个作为查询范围:

public function scopeDate($query, \Carbon\Carbon $date) {
    return $query->whereDate($date->toDateString());
}

2 个解决方案

#1


9  

You should add your column in the getDates() array. By doing this Eloquent will automatically convert your datetime to Carbon object.

您应该在getDates()数组中添加列。通过这样的方式,你可以自动将你的时间转换成碳的对象。

From the docs:

从文档:

By default, Eloquent will convert the created_at, updated_at, and deleted_at columns to instances of Carbon, which provides an assortment of helpful methods, and extends the native PHP DateTime class.

默认情况下,strate将created_at、updated_at和deleted_at列转换为Carbon实例,这提供了各种有用的方法,并扩展了本机PHP DateTime类。

See this: http://laravel.com/docs/eloquent#date-mutators

看到这个:http://laravel.com/docs/eloquent # date-mutators

Again, providing the date in the right format (Y-m-d) may still be necessary because it is not always possible to tell how to interpret a given date

同样,以正确的格式(Y-m-d)提供日期可能仍然是必要的,因为通常不可能知道如何解释给定的日期

#2


1  

As told here, Laravel uses Carbon as a DateTime class, you can perform it using:

如前所述,Laravel使用Carbon作为DateTime类,您可以使用:

Model::whereDate( \Carbon\Carbon::now()->subDays(2) )->get();

#1


9  

You should add your column in the getDates() array. By doing this Eloquent will automatically convert your datetime to Carbon object.

您应该在getDates()数组中添加列。通过这样的方式,你可以自动将你的时间转换成碳的对象。

From the docs:

从文档:

By default, Eloquent will convert the created_at, updated_at, and deleted_at columns to instances of Carbon, which provides an assortment of helpful methods, and extends the native PHP DateTime class.

默认情况下,strate将created_at、updated_at和deleted_at列转换为Carbon实例,这提供了各种有用的方法,并扩展了本机PHP DateTime类。

See this: http://laravel.com/docs/eloquent#date-mutators

看到这个:http://laravel.com/docs/eloquent # date-mutators

Again, providing the date in the right format (Y-m-d) may still be necessary because it is not always possible to tell how to interpret a given date

同样,以正确的格式(Y-m-d)提供日期可能仍然是必要的,因为通常不可能知道如何解释给定的日期

#2


1  

As told here, Laravel uses Carbon as a DateTime class, you can perform it using:

如前所述,Laravel使用Carbon作为DateTime类,您可以使用:

Model::whereDate( \Carbon\Carbon::now()->subDays(2) )->get();