如何以日期格式Laravel添加数据?

时间:2016-08-04 12:02:07

标签: laravel laravel-5.2

我有模特:

$tour = new Tour();

将数据添加到对象$tour后:

$tour->date_start_at = $request->date_start_at;
$tour->date_end_at = $request->date_end_at;
$tour->save();

数据库中的字段date_start_attour->date_end_atdatetime类型。

同样在我的模特中我有:

protected $dates = ['created_at', 'updated_at','date_start_at', 'date_end_at'];

当我尝试保存它时,我得到:

InvalidArgumentException in Carbon.php line 425:

Unexpected data found.
Unexpected data found.
Data missing

1 个答案:

答案 0 :(得分:1)

使用Carbon解析数据:

$tour->date_start_at = Carbon::parse($request->date_start_at);
$tour->date_end_at = Carbon::parse($request->date_end_at);
$tour->save();