如何更改模型的$ dates属性的格式?

时间:2016-01-20 11:59:56

标签: php laravel laravel-5 php-carbon

在我的laravel应用程序中,我有一个travel_date类型的数据库列date。此列已添加到Eloquent-Model类的$dates属性中。但是,如果我将值22.01.2015分配给模型Carbon则抛出异常

InvalidArgumentException in Carbon.php line 425: Unexpected data found.
Unexpected data found.
Data missing

in Carbon.php line 425
at Carbon::createFromFormat('Y-m-d H:i:s', '20.01.2016') in Model.php line 3015
at Model->asDateTime('20.01.2016') in Model.php line 2971
at Model->fromDateTime('20.01.2016') in Model.php line 2926
at Model->setAttribute('travel_date', '20.01.2016') in Model.php line 446
[...]

我创建模型的代码是:

$trip = Trip::create(array_merge($request->all(), ['carpool_id' => $carpool->id])); //travel_date is 20.01.2016

我的$dates - 财产

protected $dates = ['deleted_at', 'travel_date'];

致谢!

1 个答案:

答案 0 :(得分:1)

你可以这样创造旅行:

$trip = new Trip();
$trip->property_1 = $request->property_1;
$trip->property_2 = $request->property_2;
$trip->carpool_id = $carpool->id;
$trip->travel_date = Carbon::createFromFormat('d.m.Y', '20.01.2016');
$trip->save();