所以,我知道 Laravel 将它们的“created_at”和“updated_at”存储为 Carbon 对象。至少,每次我尝试获取它们时,它们都会以 Carbon 的形式返回。所以我有一个 Carbon 对象,我想将它存储在我的“premium_until”列中。现在,我应该怎么做呢?在设置架构时,我应该执行 $table->timestamp('premium_until');
还是 $table->date('premium_until');
?另外,我可以只执行 $user->premium_until = $carbonObject;
还是必须将其转换为字符串?提前致谢!
最佳答案
Eloquent 提供了处理日期的便捷方式。
只需在 Eloquent 模型上定义 getDates
方法,该方法返回列名数组,Eloquent 应自动将其转换为 Carbon
实例!
public function getDates()
{
return array(static::CREATED_AT, static::UPDATED_AT, static::DELETED_AT, 'premium_until');
}
关于php - 如何使用 Laravel 在 MySQL 中存储 Carbon 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20305194/