英文:
Unexpected data found. Trailing data laravel and carbon
问题
我想在数据库中创建'add days',但在laravel中出现了以下错误,
**意外发现数据。尾随数据**
格式列created_at为datetime
$packages = Package::find($id);
$licenceF = Licence::where('tbl_package_id' , $id)
->select('created_at')
->get();
$date = Carbon::createFromFormat('Y-m-d', $licenceF);
$findPackage = Package::where('id' , $request->input('tbl_package_id'))->pluck('time');
$licenceF = $licenceF->addDays($findPackage);
英文:
I want 'add days' to be created in database, but got this error in laravel,
Unexpected data found. Trailing data
format column created_at is datetime
$packages = Package::find($id);
$licenceF = Licence::where('tbl_package_id' , $id)
->select('created_at')
->get();
$date = Carbon::createFromFormat('Y-m-d', $licenceF);
$findPackage = Package::where('id' , $request->input('tbl_package_id'))->pluck('time');
$licenceF = $licenceF->addDays($findPackage);
答案1
得分: 0
使用 Carbon::parse()
替代 Carbon::createFromFormat()
,如下所示:
$licenceF = Carbon::parse($findPackage[0]->created_at)->addDays($findPackage);
您还可以将查询结果作为数组获取,如下所示:
$licenceF = License::where('tbl_package_id', $id)->select('created_at')->get()->toArray()
然后可以使用:
$licenceF[0]['created_at']
parse
和 createFromFormat
之间的区别在于,第二种方法尝试“猜测”您作为参数传递的日期字符串的格式。如果您有自己的日期处理类,这可能会很有用。
我希望这些信息对您有帮助。
英文:
Use Carbon::parse()
instead of Carbon::createFromFormat()
as follows :
$licenceF = Carbon::parse($findPackage[0]->created_at)->addDays($findPackage);
You can get the query result as an array as well using :
$licenceF = License::where('tbl_package_id' , $id)->select('created_at')->get()->toArray()
and later using
$licenceF[0]['created_at']
The difference between parse
and createFromFormat
is that the second method tries to "guess" the format of the date string you pass as an argument. This might be handy if you have your own class for date manipulation.
I hope I was helpful
答案2
得分: 0
以下是您要翻译的代码的中文翻译:
// 我编写了这段代码来更改每个许可证的创建日期,但数据库中第一行的日期将更新为所有列
foreach ($licenceF as $items){
$items = Carbon::parse($items->created_at)->addDays($findPackage->time);
$items->format('Y-m-d');
if($items){
$newdate = Licence::where('tbl_package_id', '=', $id)->update([
'tbl_package_id' => $request->input('tbl_package_id'),
'expires_at' => $items,
]);
}
}
请注意,我只翻译了代码部分,没有包含其他内容。
英文:
I write this code for change each license created_at, but date of first row in database update for all column
foreach ($licenceF as $items){
$items = Carbon::parse($items->created_at)->addDays($findPackage->time);
$items->format('Y-m-d');
if($items){
$newdate = Licence::where('tbl_package_id', '=', $id)->update([
'tbl_package_id' => $request->input('tbl_package_id'),
'expires_at' => $items,
]);
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论