Reputation: 23
I'm using laravel 5.1 with eloquent, i have a postgre database and i would like to insert an object with some date in. My start format date is like '15/10/2015', and i need to convert into 'Y-m-d'.
For that i use date mutators in my model, i add this line :
protected $dates = ['created_at', 'updated_at', 'prj_start_date', 'prj_end_date_init'];
I use an architecture with services and repositories so in my repository for create my object in DB i have this :
$this->model->create($data);
When i try to insert i have this error
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', '15/10/2015') in Model.php line 2959
at Model->asDateTime('15/10/2015') in Model.php line 2915
at Model->fromDateTime('15/10/2015') in Model.php line 2870
at Model->setAttribute('prj_start_date', '15/10/2015') in Model.php line 422
Why isn't work, i've miss something ? Can someone help me please ?
Upvotes: 1
Views: 1065
Reputation: 1695
In at Carbon::createFromFormat('Y-m-d H:i:s', '15/10/2015') in Model.php line 2959
it is pretty clear that somehow laravel hardcoded it's dates mutator. I suggest using a manual approach, converting your string into Carbon
instance via:
Carbon::createFromFormat($format, $time, $tz);
for example assuming have a date field instead of timestamp:
Carbon::createFromFormat('d/m/Y', '15/10/2015');
ps. i did not have the time to test this yet. Nevertheless, learning from the source is much interesting right?
ps. you might interested in changing laravel's format just like the docs however, i suspect this will affect created_at
and updated_at
(also deleted_at
) fields that by default is a timestamp instead of dates.
ps. right, i do forget to mention you had to include Carbon to your controller, just add use Carbon/Carbon
Upvotes: 1