Laravel model Trailing Data when save the model

前端 未结 6 780
青春惊慌失措
青春惊慌失措 2021-01-02 02:00

I have some code like this

    $editStuState = StuAtt::where(\'studentId\' , \'=\' , $id)->first();
    $editStuState -> leave +=1;
    $editStuState -         


        
相关标签:
6条回答
  • 2021-01-02 02:28

    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

    0 讨论(0)
  • 2021-01-02 02:29

    Trailing data is a Carbon error, it's because you probably use Postgres and your 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';
    }
    

    If you are using TIME WITH TIMEZONE in Postgres then the format should be:

    Y-m-d H:i:s.uO
    
    
    0 讨论(0)
  • 2021-01-02 02:29

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

    0 讨论(0)
  • 2021-01-02 02:30

    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

    0 讨论(0)
  • 2021-01-02 02:38

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

    Change it to 0 and save changes:

    0 讨论(0)
  • 2021-01-02 02:52

    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.

    0 讨论(0)
提交回复
热议问题