Ron Brouwers Ron Brouwers - 27 days ago 20
PHP Question

Laravel Carbon Data Missing

In my model I have the following:

protected $dates = [
'start',
'end',
'created_at',
'updated_at'
];


I am using a datetime picker to insert the start and end dates, in this format:

2016-01-23 22:00


Without the seconds. When I do it like this, I get this error:

InvalidArgumentException in Carbon.php line 425:
Data missing
at Carbon::createFromFormat('Y-m-d H:i:s', '2016-01-23 22:00') in Model.php line 3015


If I do include the seconds, it works. The seconds are not important to me, and I do not want to include them in my datetime picker fields. Any way around this so I can still use those fields as date fields?

Answer

You can set the $dateFormat in your model as Christian says, but if you don't want to imply the updated_at and created_at fields into the operation you can use events to "correct" the datetime object before saving it into the database.

Here you have the official doc about it: https://laravel.com/docs/5.2/eloquent#events