Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel Trailing Data Exception when model save or update

I have problem with update the model data in laravel 5.6, After many time I find that actually problem is with created_at and updated_at. My code:

$editStuState = StuAtt::where('studentId' , '=' , 1007)->first();
dd($editStuState -> created_at);

and dd($editStuState)

StuAtt {#382 ▼
  #table: "stu_attendance"
  #connection: "mysql"
  #primaryKey: "id"
  #keyType: "int"
  +incrementing: true
  #with: []
  #withCount: []
  #perPage: 15
  +exists: true
  +wasRecentlyCreated: false
  #attributes: array:7 [▼
    "id" => "3"
    "studentId" => "1007"
    "present" => "7"
    "absent" => "2"
    "leave" => "6"
    "created_at" => "2018-04-19 07:01:19.929554"
    "updated_at" => "2018-04-19 02:31:19.000000"
  ]
  #original: array:7 [▼
    "id" => "3"
    "studentId" => "1007"
    "present" => "7"
    "absent" => "2"
    "leave" => "6"
    "created_at" => "2018-04-19 07:01:19.929554"
    "updated_at" => "2018-04-19 02:31:19.000000"
  ]
  #changes: []
  #casts: []
  #dates: []
  #dateFormat: null
  #appends: []
  #dispatchesEvents: []
  #observables: []
  #relations: []
  #touches: []
  +timestamps: true
  #hidden: []
  #visible: []
  #fillable: []
  #guarded: array:1 [▶]
}

Error that appears when I print the created_at field:

InvalidArgumentException
Trailing data

Where is mistake and how fix it?

like image 800
Nasser Ali Karimi Avatar asked May 07 '18 08:05

Nasser Ali Karimi


1 Answers

Trailing data is a Carbon error, it's because you probably use PostgreSQL, and DB's date returns milliseconds.

"created_at" => "2018-04-19 07:01:19.929554"

You can add the following method to your (base) model.

    // ...

    public function getDateFormat()
    {
         return 'Y-m-d H:i:s.u';
    }
}
like image 156
Robert Avatar answered Nov 14 '22 23:11

Robert