php - 使用 Eloquent/Laravel 存储日期

标签 php mysql laravel eloquent

在 Eloquent 模型中存储日期的好方法是什么?我在 Laravel 框架中使用 PHP。

class MyModel extends Eloquent {
  // I want a date field here... should it be:
  public $endTime = ?;
}

我应该使用保存为 int 的整数时间戳吗?如果我在属性中使用 Date 对象并保存该对象,会有什么行为?

最佳答案

如果您使用 migration然后使用 $table->timestamp('endTime'),这将使用 endTime 作为名称创建一个 timestamp 字段。

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. You may customize which fields are automatically mutated, and even completely disable this mutation, by overriding the getDates method of the model.

在你的模型中添加这个方法:

public function getDates()
{
    return array('created_at', 'updated_at', 'deleted_at', 'endTime');
}

Laravel 将处理这些字段,默认情况下,每个字段都是 Carbon 的实例。目的。了解 date mutators .在这种情况下,Carbon 的所有方法都可以用于这些字段。

关于php - 使用 Eloquent/Laravel 存储日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23857695/

相关文章:

linux - mysqld 服务每天在 ec2 服务器上停止一次

node.js - 为什么 webpack 在运行 npm run dev 时会报错

php - Laravel 请求具有复杂 url 的通配符

php - 使用 Laravel 和 AngularJS 从数据库获取用户特定信息

php - 如何更新表行并在行字段中添加+1?

PHP如何执行传递cookie的http请求并将结果保存到字符串

MySQL查询返回带有详细/重复信息的数据

mysql - SQL查询多表的问题

javascript - Vue.js 中未定义的组件

php - Xml 到 Mysql fatal error