1

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 ?

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks but i want convert my date in controller (i have a lot of date). I solve my problem. I add a mutator for all my date.code : private function setPrjStartDateValue($value) { $date_parts = explode('/', $value); $this->attributes['prj_start_date'] = $date_parts[2].'-'.$date_parts[0].'-'.$date_parts[1]; }
well, that's one of the solutions (but not the one i think of). glad you sort it out.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.