Home > Blockchain >  How can I save 2021-04-08T22:25:09.335541Z as timestamp to database in Laravel?
How can I save 2021-04-08T22:25:09.335541Z as timestamp to database in Laravel?

Time:12-16

I have a date string but this date's format looks like 2021-04-08T22:25:09.335541Z I couldn't saved to database

This is error message

Illuminate\Database\QueryException: SQLSTATE[22007]: Invalid datetime format: 1292 Incorrect datetime value: '2021-04-08T22:25:09.335541Z' for column 'published_at' at row 1 

I created published_at and it's date format is timestamp

For Example $table->timestamp('published_at');

I tried use Carbon::parse($publishedAt)->timestamp;

Illuminate\Database\QueryException: SQLSTATE[22007]: Invalid datetime format: 1292 Incorrect datetime value: '1617920709' for column 'published_at' at row 1

returned it to me

CodePudding user response:

You can use Carbon to parse the date before saving it into database in Your Model:

public function setPublishedAtAttribute($value)
{
     $this->attributes['published_at'] =  Carbon::parse($value);
}
  • Related