Error while working with date formatting in Laravel

Asked

Viewed 139 times

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

1 answer

1

Laravel is doing internally the next:

isset($usuario->data_nascimento->format('d/m/Y')) ? $usuario->data_nascimento->format('d/m/Y') : old('data_nascimento');

Case format false ("Returns the Formatted date string on Success or FALSE on Failure."), or old('data_nascimento') for null, this error is shown: DOCS old ("If no old input exists for the Given field, null will be returned:").

Error example (both cases, isset(false) or isset(null) do Trigger to that mistake)

Try the following on:

<input id="data_nascimento" type="date" placeholder="dd/mm/yyyy" class="form-control{{ $errors->has('data_nascimento') ? ' is-invalid' : '' }}" name="data_nascimento" value="{{!is_null(old('data_nascimento')) ? old('data_nascimento') : $usuario->data_nascimento->format('d/m/Y')}}">

OR:

<input id="data_nascimento" type="date" placeholder="dd/mm/yyyy" class="form-control{{ $errors->has('data_nascimento') ? ' is-invalid' : '' }}" name="data_nascimento" value="@if(!is_null(old('data_nascimento'))) {{old('data_nascimento')}} @else {{$usuario->data_nascimento->format('d/m/Y')}} @endif">
  • 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?

  • 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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.