Laravel model Trailing Data when save the model [duplicate]
Asked Answered
P

5

13

I have some code like this

    $editStuState = StuAtt::where('studentId' , '=' , $id)->first();
    $editStuState -> leave +=1;
    $editStuState -> present = $editStuState -> present-1;
    $editStuState->update();
                            //OR
    $editStuState->save();
    return 'this is good';

I can't save or Update my data, when I remove Update and Save related line it can print text.

this is the dd($editStuState) data

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" => "7"
    "studentId" => "1"
    "present" => "2"
    "absent" => "1"
    "leave" => "10"
    "created_at" => "2018-04-16 11:17:41.176898"
    "updated_at" => "2018-04-16 06:47:41.000000"
  ]
  #original: array:7 [▼
    "id" => "7"
    "studentId" => "1"
    "present" => "2"
    "absent" => "1"
    "leave" => "10"
    "created_at" => "2018-04-16 11:17:41.176898"
    "updated_at" => "2018-04-16 06:47:41.000000"
  ]
  #changes: []
  #casts: []
  #dates: []
  #dateFormat: null
  #appends: []
  #dispatchesEvents: []
  #observables: []
  #relations: []
  #touches: []
  +timestamps: true
  #hidden: []
  #visible: []
  #fillable: []
  #guarded: array:1 [▶]
}

I also got this error form laravel 5.6 it

InvalidArgumentException
Trailing data
Premonish answered 7/5, 2018 at 7:17 Comment(0)
H
9

If you are using Postgres you have to add some lines to your Model(s). It happens because of TIME WITH TIMEZONE in Postgres.

Please also read Date Mutators as Laravel already has support for this baked in, simply put below line in your Model to override the default dateFormat for that model: https://laravel.com/docs/5.7/eloquent-mutators#date-mutators

Go to your App/Model (under app folder, exp. User, SomeModel) add below line:

protected $dateFormat = 'Y-m-d H:i:sO';

Best

Hallucinogen answered 3/6, 2019 at 21:9 Comment(0)
T
1

Change your code, from:

$editStuState = StuAtt::where('studentId' , '=' , $id)->first();
$editStuState -> leave +=1;
$editStuState -> present = $editStuState -> present-1;
$editStuState->update();
                        //OR
$editStuState->save();
return 'this is good';

To:

$editStuState = StuAtt::where('studentId' , '=' , $id)->first();
$editStuState -> leave +=1;
$editStuState -> present = $editStuState -> present-1;
$editStuState->save();
return 'this is good';

Method ->update(...) is used for mass updates, check Mass Updates

Triphthong answered 7/5, 2018 at 7:33 Comment(2)
I say that I check both of them and get same errors.Do you see //OR?Premonish
Try adding protected $dateFormat = 'Y-m-d H:i:s'; to your modelTriphthong
T
1

If your Database is Postgres and your field is Timestamp sometimes Carbon cannot convert to default format (without milliseconds).

If milliseconds is not needed, update field content to not have the millisecond part.

UPDATE YOURTABLE SET created_at = date_trunc('seconds', created_at),
updated_at = date_trunc('seconds', updated_at)

This will normalize timestamped fields without milliseconds.

Transit answered 20/12, 2018 at 21:4 Comment(0)
C
0

I have the same problem, but I changed the column name to creation_date , the problem solved.

Caboodle answered 21/1, 2020 at 5:39 Comment(0)
W
0

In my case problem was length of created_at and updated_atin table. In Navicat table design was like this:

enter image description here

Change it to 0 and save changes:

enter image description here

Walkup answered 26/10, 2020 at 9:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.