I am a Laravel newbie so please forgive me for my code... I have a method, in a Laravel controller, that updates the current logged user profile (postProfileGeneral()). Logged user can upload a profile picture, and I manipulate the uploaded file (i.e. remove spaces, create unique name, etc.); since I'd like to reuse the upload routine, I put it on a separate method (_upload()) The code is this:

// get a unique file name
protected function _getUniqueFilename($path, $filename, $extension)
    $i = 2;
    $output = $filename . '.' . $extension;
    while(File::exists($path . '/' . $output)){
        $output = $filename . '_' . $i . '.' . $extension;
    return $output;

// upload method, to reuse the routine
protected function _upload(Request $request, $field, $path = '')
    $path = ($path != '') ? $path : public_path();
    if ($request->hasFile($field)){
        $file = $request->file($field);
        if ($file->isValid()){
            $basename = $file->getClientOriginalName(); // get the original filename + extension
            $extension = $file->getClientOriginalExtension(); // get the original extension without the dot
            $filename = basename($basename, '.' . $extension); // get the original filename only
            $slug = str_slug($filename, '-'); // slug the original filename
            $name = $this->_getUniqueFilename($path, $slug, $extension);
            $file->move($path, $name);
            $output = $path . $name;
    } else {
        $output = $request->input('cur_picture');
    return $output;

// update profile
public function postProfileGeneral(Request $request)
    $picture = $this->_upload($request, 'picture', config('custom.users_path'));
    // rest of method here, not relevant

It works fine, but I feel there is a better way of doing this My questions are:

  1. since the getUniqueFilename() and _upload() method can be reused in other controllers too (i.e. for uploading products pictures, or whatever) where should I put them to be generally available?
  2. in the getUniqueFilename() method I must pass Request object as a parameter: is there a better way to do this? At last, I yet have my Request object available in the postProfileGeneral() method...

1 Answer 1


I think your program is good. I repeat, IMO, it's good.
As you said in questions, the answers are

  1. Move your image manipulation related methods to a TRAIT. that'll help you while using the same code in different controllers. Because, if you want it to be done like this way, you must have to declare objects, if you want to use the same code from different controllers/classes. On the other hand, if you use TRAIT, you just have to use them.
  2. As you're passing the Request to your method, you may pass the array instead. Otherwise, you may use check the file existence, on the previous method then pass the values to next method. You may wish to use varargs as a method parameter.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.