Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unexpected data found during save on eloquent / Laravel

I have one more field on the database over the created_at and updated_at as TIMESTAMP the field name is date.

So i overwritten the method getDates() on my model eloquent because i wanted that field be instantiated from Carbon.

public function getDates()
{
   return ['date','created_at','updated_at'];
}

But when i go to create a new record on the database it throw me an exception:

InvalidArgumentException Unexpected data found. Unexpected data found. Unexpected data found.

Ps: the value sent from the form is in EU format: d-m-Y h:i

I don't know how figure out this problem any suggestion are appreciated

like image 379
Fabrizio Fenoglio Avatar asked Apr 13 '14 12:04

Fabrizio Fenoglio


2 Answers

You array returned from getDates was merged with the dafault one resulting in:

['created_at','updated_at','deleted_at','date','created_at','updated_at'];

so use only 'date' there and should be fine.


Try setting up a mutator for 'date' to convert the data from input into timestamp format. The error you get is not on Eloquent but Carbon.

public function setDateAttribute($value)
{
    $this->attributes['date'] = Carbon\Carbon::createFromFormat('d-m-Y h:i', $value);
}

Also there is mistake in the docs, as getDates defines date accessors, not mutators..

like image 67
Jarek Tkaczyk Avatar answered Sep 20 '22 17:09

Jarek Tkaczyk


Try this:

Carbon::createFromFormat('d.m.Y H:i', $request->publishdate);    
like image 1
Doğuhan Elma Avatar answered Sep 19 '22 17:09

Doğuhan Elma