0
I’m having a little trouble manipulating a input of the kind date. When using this type of input the date is formatted as Y-m-d, but in my form I want to type dd/mm/yyyy.
Model User:
//...
protected $dates = [
'data_nascimento'
];
Input ():
<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 to the input I performed directly in the controller and passed 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'));
Already when I will save the date in the bank do only a parameter inversion:
$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 data_birth returns null
Show, I forgot to mention that there are cases where data_birth will be full, there is error. Already inside the controller, before recording in the bank I have to do so
\Carbon\Carbon::createFromFormat('d/m/Y', $request->data_nascimento)->format('Y-m-d')
or would have otherwise?– Fábio Jânio
To write in BD you know the format that suits you, I save with the default type of Adjustable, which is
Y-m-d H:i:s
, https://ideone.com/b4M8DF– Miguel