Rodri6uez
Rodri6uez

Reputation: 477

Unexpected data found.\nTrailing data

I got this error:

"Unexpected data found.\nTrailing data"

It says that it comes from this LN on my update function:

$fe_ini = Carbon::createFromFormat('d/m/Y H:i', $request->fecha_inicial);

fecha_inicial comes from my blade:

<div class="col-md-4">
  <label>Fecha inicial</label>
  <input type="datetime-local" name="fecha_inicial" value="2018-01-31T18:00:00" class="form-control input-lg" ng-model="descuento.fecha_inicial" ng-disabled ="desAll" required>
</div>

My store function says:

$fecha_inicial  = Carbon::createFromFormat('d/m/Y H:i', $request->fecha_inicial);

Could you help me to find out what am I doing wrong?

Thank you.

Upvotes: 0

Views: 1281

Answers (2)

Rodri6uez
Rodri6uez

Reputation: 477

I solved it by doing this:

$fecha_inicial  = Carbon::createFromFormat('d/m/Y, H:i:s', $request->fecha_inicial)->format('Y-m-d H:i:s');

I checked my Payload and the data came with a comma ',' right after the year like this:

'13/09/2022, 11:38:53'

So what I did was to add a comma too in the format from createFromFormat method. Plus, I added ->format('Y-m-d H:i:s') to convert it.

Thanks.

Upvotes: 0

Joundill
Joundill

Reputation: 7594

As Lagbox says in their comment, your date formats don't match:

The value on your input, 2018-01-31T18:00:00, is not in the format you're passing to createFromFormat() which is 'd/m/Y H:i'.

I suggest just moving to Carbon::parse():

$fecha_inicial  = Carbon::parse($request->fecha_inicial);

Upvotes: 2

Related Questions