gpt4 book ai didi

php - 模型保存或更新时 Laravel 尾随数据异常

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:07:30 28 4
gpt4 key购买 nike

我在 Laravel 5.6 中更新模型数据时遇到问题,很多次后,我发现实际上问题出在 created_at 和 updated_at 上。我的代码:

$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字段时出现的错误:

InvalidArgumentException
Trailing data

错在哪里,如何改正?

最佳答案

尾随数据是 Carbon 错误,这是因为您可能使用 PostgreSQL,而 DB 的日期返回毫秒。

"created_at" => "2018-04-19 07:01:19.929554"

您可以将以下方法添加到您的(基础)模型中。

    // ...

public function getDateFormat()
{
return 'Y-m-d H:i:s.u';
}
}

关于php - 模型保存或更新时 Laravel 尾随数据异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50210435/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com