3

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?

samuel toh
  • 6,836
  • 21
  • 71
  • 108
  • 2
    Where is `$photo` being defined? You have the `createProduct` method type hinted so that the parameter *MUST* be an instance of `UploadedFile`. – Jeremy Harris May 17 '17 at 13:32
  • @Jeremy Harris, It defined in controller. If `$photo` like this : https://postimg.org/image/47t72locf/, no error. But `$photo` like this : https://postimg.org/image/58qo0i8u7/, there exist error – samuel toh May 17 '17 at 14:43
  • As Jeremy sais, it must be an UploadedFile Instance. Could you provide us more controller code? – Albeis May 17 '17 at 14:55
  • I think you are trying to do a multi-upload? If so, then in your controller, you need to loop over that array of photos, and for each one of them, call `product_service->createProduct(..., ...)` – Dan Costinel May 18 '17 at 07:18

1 Answers1

1

The issue here is that you are either getting a single file, or an array of files. Does creating a product support uploading more than one file? If not, you can do something like this:

// If file is inside an array, pull it out
if (is_array($photo) && $photo[0] instanceof UploadedFile) {
    $photo = $photo[0];
}

// Run upload
$result = $this->product_service->createProduct($param, $photo);

If you require the ability to upload multiple files, a few things would need to happen. First, you can't typehint your method as UploadedFile. Instead, typehint it as an array:

public function createProduct($param, array $photos = [])

And then in your createProduct logic, you will need to loop through the array of photos and save them individually.

Finally, in your controller, you will need to do the opposite of the first suggestion and instead of extracting from an array, you need to put it in one:

if ($photo instanceof UploadedFile) {
    $photo = [$photo];
}
Jeremy Harris
  • 24,318
  • 13
  • 79
  • 133