Hey Prez. The thing is I'm not calling that createFromFormat() function directly, it's being called through the Laravel framework when I do the ->toArray() call in the Form. I suppose if I set $format = Y-m-d H:i:s it will work, but I wanted to use the d-m-Y format.
Date handling - Carbon and formatting
I've done quite a bit of googling, and experimenting with my codebase, but I can't seem to get the date handling right. I simply want to store my dates in my model as Carbon instances, so I have them in the $dates variable. I have my preferred dateFormat, and the casts variable, apparently to get data entered using the dateFormat specified, automatically converted back to Carbon instances?
protected $dateFormat = 'd-m-Y';
protected $dates = ['invoice_date', 'due_date', 'created_at', 'updated_at', 'deleted_at'];
protected $casts = ['invoice_date' => 'date', 'due_date' => 'date'];
But when I show my view, I get an Exception in Carbon.php line 425 - 'The separation symbol could not be found' in the createFromFormat function
public static function createFromFormat($format, $time, $tz = null)
{
if ($tz !== null) {
$dt = parent::createFromFormat($format, $time, static::safeCreateDateTimeZone($tz));
} else {
$dt = parent::createFromFormat($format, $time);
}
if ($dt instanceof DateTime) {
return static::instance($dt);
}
$errors = static::getLastErrors();
throw new InvalidArgumentException(implode(PHP_EOL, $errors['errors']));
}
The error happens because the above function receives $format = 'd-m-Y', $time = '2016-04-08 00:00:00', so it can't convert it.
The view is using form-model binding, and I'm converting the input to an array to get the dates to be formatted correctly:
!! Form::model($invoice->toArray(), ['method' => 'GET', 'url' => 'invoice/'.$invoice->id.'/edit']) !!}
I had a workaround before where I forced my model to always return my dates as a formatted string in my preferred format using an accessor, but that doesn't seem right. Isn't that was the $dateFormat property does?
Please or to participate in this conversation.