Laravel Trailing Data Exception when model save or update(模型保存或更新时 Laravel 尾随数据异常)
问题描述
我在 Laravel 5.6 中更新模型数据时遇到问题,很多次之后,我发现实际上问题出在 created_at 和 updated_at 上.我的代码:
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);
 和 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 [▶]
}
打印 created_at 字段时出现的错误:
Error that appears when I print the created_at field:
InvalidArgumentException
Trailing data
错误在哪里以及如何解决?
Where is mistake and how fix it?
推荐答案
拖尾数据是 Carbon 错误,因为你可能使用了 PostgreSQL,而 DB 的日期返回毫秒.
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"
"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';
    }
}
                        这篇关于模型保存或更新时 Laravel 尾随数据异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:模型保存或更新时 Laravel 尾随数据异常
				
        
 
            
        基础教程推荐
- php中的PDF导出 2022-01-01
 - php 7.4 在写入变量中的 Twig 问题 2022-01-01
 - Web 服务器如何处理请求? 2021-01-01
 - 将变量从树枝传递给 js 2022-01-01
 - 使用 scandir() 在目录中查找文件夹 (PHP) 2022-01-01
 - 主题化 Drupal 7 的 Ubercart “/cart"页 2021-01-01
 - Yii2 - 在运行时设置邮件传输参数 2022-01-01
 - 如何在数学上评估像“2-1"这样的字符串?产生“1"? 2022-01-01
 - php中的foreach复选框POST 2021-01-01
 - PHPUnit 的 Selenium 2 文档到底在哪里? 2022-01-01
 
    	
    	
    	
    	
    	
    	
    	
    	
				
				
				
				