I have a timestamp in the DB and am trying to get the swine to save. I either get an issue where the MySQL
timestamp is blank or I get an error. The format posted is UK standard.
Posting data, I have tried some of the following:
$user['crb_date'] = Carbon::createFromFormat('d/m/Y', $data['crb_date']); $user['crb_date'] = Carbon::createFromFormat('d/m/Y', $data['crb_date'])->toDateTimeString(); // Plus many other more std date(), strtodate() combos
On my Model, I have custom getDates which will convert the dates to Carbon:
public function getDates() { return [ 'last_login', 'created_at', 'updated_at', 'dob', 'crb_date' ]; }
Now, if I update the user with the carbon date, I get the following issue:
Unexpected data found. Unexpected data found. Data missing
However, if I dd($user['crb_date']
after the second Carbon date conversion I get
string '2011-05-01 11:20:23' (length=19)
Which looks pretty good to me.
If I remove the accessor on the Model, I get it to post, however, I get the blank timestamp
0000-00-00 00:00:00
I've also tried putting a mutator on the model to set the date there, but issue is exactly the same.
Any ideas what I can do to get this to work? I think it might be something to do with the accessor causing the issue, but need that to work with the dates better afterwards.
Many thanks for taking the time to help.
This is the mutator I have tried - It was set on the Model. However, the above code (Carbon::createFormFormat... is currently in my repo:
public function setCrbDateAttribute($value) { $this->attributes['crb_date'] = \Carbon\Carbon::createFromFormat('d-m-Y h:i', $value); }
The Accessor:
public function getDates() { return [ 'last_login', 'created_at', 'updated_at', 'dob', 'crb_date' ]; }
SORTED!
Thanks to WereWolf's answer below, I tweaked the Model mutator and it is now working like a charm. Here is what I have now:
public function setCrbDateAttribute($value) { $this->attributes['crb_date'] = \Carbon\Carbon::createFromFormat('d/m/Y', $value)->toDateTimeString(); }