Search code examples
phplaravellaravel-controller

Method break after calling function in same controller


I made a method that handles image. The problem is I want to call that method in my Edit method few times because there are 3 buttons for uploading image. But after calling my method for IMAGE the EDIT method just stops after first image. How can I stop that?

if($request->has('picture1')) {
    return $this->upload_image($company, $asset, $request->picture1);
}
if($request->has('picture2')) {
    return $this->upload_image($company, $asset, $request->picture2);
}
if($request->has('picture3')) {
    return $this->upload_image($company, $asset, $request->picture2);
}

And this is my function for uploading image

public function upload_image($company, $asset, $image) 
    {
        $filename = uniqid(rand());
        $image = str_replace('data:image/png;base64,', '', $image);
        $image = str_replace(' ', '+', $image);
        if (!file_exists(public_path('/uploads/assets/pictures/' . $company->id . '/'))) {
            File::makeDirectory(public_path('/uploads/assets/pictures/' . $company->id . '/'), 0777, true);
        }
        Image::make($image)->resize(1024, null, function ($constraint) { $constraint->aspectRatio();})
            ->save( public_path('/uploads/assets/pictures/' . $company->id . '/'. $filename ) );
    }

How can i go back to my function after handling first image?


Solution

  • You have to simply remove the return keyword like this :

    if($request->has('picture1')) {
        $this->upload_image($company, $asset, $request->picture1);
    }
    if($request->has('picture2')) {
        $this->upload_image($company, $asset, $request->picture2);
    }
    if($request->has('picture3')) {
        $this->upload_image($company, $asset, $request->picture2);
    }