意外发现数据。尾随数据 laravel 和 carbon

huangapple go评论62阅读模式
英文:

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

意外发现数据。尾随数据 laravel 和 carbon

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']

parsecreateFromFormat 之间的区别在于,第二种方法尝试“猜测”您作为参数传递的日期字符串的格式。如果您有自己的日期处理类,这可能会很有用。

我希望这些信息对您有帮助。

英文:

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,
            ]);
        }
    }

huangapple
  • 本文由 发表于 2023年7月3日 16:58:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/76603289.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定