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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19275263/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 19:10:27  来源:igfitidea点击:

Querying a date column with Laravel's query builder

phplaravellaravel-4eloquent

提问by giaour

When writing a query against a table with a date column using the Laravel query builder, what data type should be used? The following shouldreturn 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->format("Y-m-d"))->get()改为有效,但似乎不像 Laravel 那样做。

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 认为日期列是一个字符串:Model::lists('date')返回一个字符串数组,就像DB::table('table')->lists('date').

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

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

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

更新:无论出于何种原因,内置的 MySQL dateto\Carbon\Carbon对我不起作用,所以最简单的解决方案是编写一个作为查询范围:

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

回答by Glad To Help

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

您应该在 getDates() 数组中添加您的列。通过这样做,Eloquent 会自动将您的日期时间转换为 Carbon 对象。

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.

默认情况下,Eloquent 会将 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

同样,以正确的格式 (Ymd) 提供日期可能仍然是必要的,因为并不总是可以告诉如何解释给定的日期

回答by Antonio Carlos Ribeiro

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();