1

我正在尝试将日期输入转换为正确的 mysql 格式。我的 start_date 字段格式为 MM dd, yyyy。

在我的控制器存储功能中,我正在获取所有用户输入,包括 start_date 和保存。

$userProfileData->save();

我想我能做的是我们 Carbon 在保存之前转换 start_date 像这样。

$userProfileData->start_date->toDateString();

我在非对象错误上收到对成员函数 toDateString() 的调用。

4

2 回答 2

5

通过将其添加到我的用户控制器存储功能中,我能够转换日期格式。

$userProfileData->start_date = date('Y-m-d', strtotime($userProfileData->start_date));
于 2014-09-17T15:52:41.803 回答
4

你得到非对象错误,因为 start_date 不是方法 toDateString() 的对象

您可以做的是在模型中使用 setStartDateAttribute() 方法,每次都会调用该方法以获取 start_date 属性值,在此方法中您可以修改值,因此模型将使用更新后的值进行编辑或更新等操作跟随

public function setStartDateAttribute($value) {
      $this->attributes['start_date'] = date('Y-m-d', strtotime($value) );
}

当您的模型需要获取 start_date 属性的值时,将使用以下方法此方法必须写入模型中

于 2014-09-17T13:16:25.593 回答