gpt4 book ai didi

php - Laravel Eloquent - 日期属性未作为 Carbon 对象检索

转载 作者:可可西里 更新时间:2023-11-01 06:44:41 27 4
gpt4 key购买 nike

我在 Quota Eloquent 模型上有我的 $dates 属性,如下所示:

protected $dates = ['start', 'end'];

从数据库中检索数据时,这些字段不会被设置为 Carbon 实例,而是字符串。 created_atupdated_at 属性也是如此。

知道会发生什么吗?我在 MySQL 数据库上使用 TIMESTAMPS 字段,Laravel 版本是 5.2。

最佳答案

在您的模型中尝试此代码:

public function getStartAttribute($date)
{
return Carbon::parse($date);
}

public function getEndAttribute($date)
{
return Carbon::parse($date);
}

这里的约定是先写get,然后写字段名和attribute。他们应该用驼峰式写在一起。因此,在您的情况下,方法名称变为 getStartAttributegetEndAttribute

关于php - Laravel Eloquent - 日期属性未作为 Carbon 对象检索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36022334/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com