保存模型时 Laravel 模型尾随数据

Laravel model Trailing Data when save the model(保存模型时 Laravel 模型尾随数据)
本文介绍了保存模型时 Laravel 模型尾随数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我有一些这样的代码

    $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.

这是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" => "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 []
}

我也从 laravel 5.6 得到这个错误

I also got this error form laravel 5.6 it

InvalidArgumentException
Trailing data

推荐答案

如果您使用 Postgres,则必须向模型添加一些行.这是因为 Postgres 中的 TIME WITH TIMEZONE.

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

另请阅读 Date Mutators 因为 Laravel 已经支持此功能,只需在您的模型中放置以下行以覆盖该模型的默认日期格式:https://laravel.com/docs/5.7/eloquent-mutators#date-mutators

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

转到您的应用程序/模型(在 app 文件夹下,exp. User,SomeModel)添加以下行:

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

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

最佳

这篇关于保存模型时 Laravel 模型尾随数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

In PHP how can you clear a WSDL cache?(在 PHP 中如何清除 WSDL 缓存?)
failed to open stream: HTTP wrapper does not support writeable connections(无法打开流:HTTP 包装器不支持可写连接)
Stop caching for PHP 5.5.3 in MAMP(在 MAMP 中停止缓存 PHP 5.5.3)
Caching HTTP responses when they are dynamically created by PHP(缓存由 PHP 动态创建的 HTTP 响应)
Memcached vs APC which one should I choose?(Memcached 与 APC 我应该选择哪一个?)
What is causing quot;Unable to allocate memory for poolquot; in PHP?(是什么导致“无法为池分配内存?在 PHP 中?)