13

我有一些这样的代码

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

我无法保存或更新我的数据,当我删除更新和保存相关行时,它可以打印文本。

这是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 中得到了这个错误

InvalidArgumentException
Trailing data
4

5 回答 5

8

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

另请阅读Date MutatorsLaravel 已经支持此功能,只需在您的模型中添加以下行以覆盖该模型的默认 dateFormat:https ://laravel.com/docs/5.7/eloquent-mutators#date-mutators

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

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

最好的

于 2019-06-03T21:09:01.927 回答
1

如果您的数据库是 Postgres 并且您的字段是 Timestamp,有时 Carbon 无法转换为默认格式(没有毫秒)。

如果不需要毫秒,则更新字段内容以不包含毫秒部分。

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

这将在没有毫秒的情况下标准化带时间戳的字段。

于 2018-12-20T21:04:48.703 回答
1

更改您的代码,从:

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

至:

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

Method ->update(...)用于批量更新,检查Mass Updates

于 2018-05-07T07:33:13.827 回答
0

created_at在我的情况下,问题是updated_at表格的长度。在 Navicat 中的表格设计是这样的:

在此处输入图像描述

将其更改为 0 并保存更改:

在此处输入图像描述

于 2020-10-26T09:54:58.173 回答
0

我有同样的问题,但我将列名更改为creation_date,问题解决了。

于 2020-01-21T05:39:55.567 回答