lavarel里Eloquent ORM自动维护的updated_at,created_at,deleted_at

来源:互联网 发布:道路 检测 数据库 编辑:程序博客网 时间:2024/06/06 01:13

Eloquent 会认为在你的数据库表有 created_at 和 updated_at 字段,如果不想自动维护,可以在模型中添加

public $timestamps = false;

虽然在添加和更新数据库的时候很方便,但是当我取出这三种时间数据的时候需要进行处理才能得到想要的Y-m-d H:i:s格式的数据。
通过类似:

$order = Order::find(12);$create = $order->created_at;

这时 $order,获取到的并不是标准的时间格式,而是一个Carbon\Carbon 实例

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

所以当你不想改变配置的情况下,你可以使用Carbon\Carbon 类中的方法进行格式转换:

$order->created_at->toDateTimeString();//Y-m-d H:i:s$order->created_at->format('Y-m-d H:i:s');  // 返回指定格式
原创粉丝点击