skip to Main Content

I want add days to created at in database but get this error in laravel,

Unexpected data found. Trailing data

enter image description here

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);

2

Answers


  1. Chosen as BEST ANSWER

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

  2. 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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search