I'm having a hard time manipulating an input of type date . When using this type of input the date is formatted as Y-m-d, however, in my form I want to type dd / mm / yyyy.
Model User:
//...
protected $dates = [
'data_nascimento'
];
Input (blade):
<input id="data_nascimento" type="date" placeholder="dd/mm/yyyy" class="form-control{{ $errors->has('data_nascimento') ? ' is-invalid' : '' }}" name="data_nascimento" value="{{ $usuario->data_nascimento->format('d/m/Y') or old('data_nascimento') }}">
Error:
Symfony \ Component \ Debug \ Exception \ FatalErrorException (E_UNKNOWN)
Cannot use isset() on the result of an expression (you can use "null !== expression" instead)
Solution (instead of applying the format in the input I made direct in the controller and I went to the view):
$usuario->data_nascimento = \Carbon\Carbon::createFromFormat('Y-m-d', $usuario->data_nascimento)->format('d/m/Y');
return view('user-profile.edit', compact('usuario'));
Now when I save the date in the database I only do a parameter change:
$request->merge(
['data_nascimento' => \Carbon\Carbon::createFromFormat('d/m/Y', $request->data_nascimento)->format('Y-m-d')]
);
But I feel like I'm using a palliative instead of the definitive / correct solution.
Note: there are cases where date_name returns null