How can I solve "Type error: Argument 2 passed to ... must be an instance of Symfony\Component\HttpFoundation\File\UploadedFile"?

Viewed 4567

My controller is like this :

public function store(CreateProductRequest $request)
{
    ...
    $result = $this->product_service->createProduct($param,$photo);
    ...
}

My service is like this :

public function createProduct($param, UploadedFile $photo=null)
{
    dd($photo);
    if($photo) {
        $fileName = str_random(40) . '.' . $photo->guessClientExtension();
        $param['photo'] = $fileName;
        $param['photo_list'] = json_encode(['id'=>1,'name'=>$fileName]);
    }
    ...
    if(is_array($result) && count($result)>1 && $result[0])
        $this->savePhoto($photo, $fileName,$result[1]->id);
    ...
    return $result;
}

private function savePhoto(UploadedFile $photo, $fileName, $id)
{
    $destinationPath = public_path() . DIRECTORY_SEPARATOR . 'img'. DIRECTORY_SEPARATOR .'products'.DIRECTORY_SEPARATOR.$id;
    $photo->move($destinationPath, $fileName);
    resize_image($fileName,250,'products',$id);
    return $fileName;
}

Symfony\Component\HttpFoundation\File\UploadedFile is like this : https://pastebin.com/KLfnXZJV

When variable $photo Not an array or just 1 data, I do dd($photo);, it display the result

But when variable $photo is array, I do dd($photo);, there exist error :

Type error: Argument 2 passed to App\Services\ProductService::createProduct() must be an instance of Symfony\Component\HttpFoundation\File\UploadedFile, array given

How can I solve this problem?

1 Answers
Related