'ErrorException : Attempt to read property "key" on string Laravel 8 Image Upload Error

/**
         * A function to update single image. Params are filename, name, directory, the image file, old images
         */
       static function singleUploadUpdate($filename, $directory, $file, $data){
            $dir = "backend/images/".$directory;
            $large_dir = $dir."/large";
            if (!empty($file)){
                if (!File::exists($dir)){
                    File::makeDirectory($dir, 0755, true);
                }
                if (!File::exists($large_dir)){
                    File::makeDirectory($large_dir, 0755, true);
                }
                $images = json_decode($data);
                foreach ($images as $key => $value){
                    File::delete("public/".$value->big_image);
                    File::delete("public/".$value->small_image);
                }
                $uploaded_file_name = $filename.rand(1,100000).".".$file->getClientOriginalExtension();
                $path = public_path($dir."/".$uploaded_file_name);
                $path2 = public_path($large_dir."/".$uploaded_file_name);
                Image::make($file->getRealPath())->save($path2);
                Image::make($file->getRealPath())->resize(250,250)->save($path);
                $files = [
                    "small_image" => $dir."/".$uploaded_file_name,
                    "big_image" => $dir."/large/".$uploaded_file_name
                ];
                return json_encode($files);
            }else{
                return $data;
            }
        }

this is my helper function to update user image, I got error on these lines :

 foreach ($images as $key => $value){

            File::delete("public/".$value->big_image);

            File::delete("public/".$value->small_image);

        }

and my image upload codes to create a user firt time is here:

/**
 * A function to upload single image. Params are filename, name, directory and a file
 */
static function singleUpload($filename, $directory, $file){
    $dir = "backend/images/".$directory;
    $large_dir = $dir."/large";
    if (!empty($file)){
        if (!File::exists($dir)){
            File::makeDirectory($dir, 0755, true);
        }
        if (!File::exists($large_dir)){
            File::makeDirectory($large_dir, 0755, true);
        }
        $uploaded_file_name = $filename.rand(1,100000).".".$file->getClientOriginalExtension();
        $path = public_path($dir."/".$uploaded_file_name);
        $path2 = public_path($large_dir."/".$uploaded_file_name);
        Image::make($file->getRealPath())->save($path2);
        Image::make($file->getRealPath())->resize(250,250)->save($path);
        $files = [
            "small_image" => $dir."/".$uploaded_file_name,
            "big_image" => $dir."/large/".$uploaded_file_name
        ];
        return json_encode($files);
    }else{
        return [];
    }
}

and I call this this functions like this

imageUpload::singleUploadUpdate(mHelper::permalink($request->job_title.rand(1,1000)), "users", $request->file("avatar"),$all_images);

so what I did wrong ? In blade template engine or somewhere else everything works fine, but while uploading image makes problem. Thank you for your help.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source