utilityLA's avatar

Adding file upload field to user registration form

I’m new at Laravel and am trying to add a file upload field to the standard registration scaffolding in Laravel 5.7. My goal is to store the uploaded file in a directory and the path name as a string in the database. I’m able to pass the file name to the database, but I’m unable to upload the actual file to a new directory. I keep receiving the error “Call to a member function getClientOriginalExtension() on null”. It seems clear that I’m not getting the upload. Thanks for your guidance.

Here’s part of the form

       <div class="">
           <input type="file" class="" id="resume" name="resume" required>
           <label class="custom-file-label" for="resume">Choose file</label>
       </div>

       @if ($errors->has('resume'))
           <span class="invalid-feedback" role="alert">
               <strong>{{ $errors->first('resume') }}</strong>
           </span>
       @endif

RegisterController.php

protected function create(array $data)
    {

        $request = request();
        $resume = $request->file('resume');
        $resumeSaveAsName = time() . Auth::id() . "-resume." . 
                                      $resume->getClientOriginalExtension();
        $upload_path = 'resume/';
        $resume_url = $upload_path . $resumeSaveAsName;
        $success = $resume->move($upload_path, $resumeSaveAsName);

        if ($data['type'] == 'provider') {

            $user = User::create([
                'fname' => $data['fname'],
                'lname' => $data['lname'],
                'email' => $data['email'],
                'phone' => $data['phone'],
                'password' => Hash::make($data['password']),
            ]);

            $providerProfile = new ProviderProfile;

            $providerProfile->fill([
                'user_id' => $user['id'],
                'resume' => $data['resume'],
            ]);

            $providerProfile->save([$user]);

            return $user;
        }

    }
0 likes
2 replies
rawilk's avatar
rawilk
Best Answer
Level 47

How are you submitting your form? Through ajax or normal request? If through a normal post request, make sure you have enctype="multipart/form-data" on your form tag, otherwise the file won't be sent through. If you're using ajax, make sure you are using FormData for sending your data through.

1 like

Please or to participate in this conversation.