Adams_'s avatar

Error Exception: "Creating default object from empty value"

I keep getting this error "Creating default object from empty value" whenever I try uploading profile image when registering user, what I want to do is to upload to the image path "profile-photos/PP_1637044275.jpg" to the database, not the image name, any help will be appreciated

This is the Controller/action class

  class CreateNewUser implements CreatesNewUsers
 {
    use PasswordValidationRules;

  /**
 * Validate and create a newly registered user.
 *
 * @param  array  $input
 * @return \App\Models\User
 */
public function create(array $input)
{

    $request = request();

    //Handle profile photo Upload
    if ($request->hasFile('photo')) {
        // Get filename with extention 
        $filenamewithExt = $request->file('photo')->getClientOriginalName();
                  // Get just filename
                   $filename = pathinfo($filenamewithExt, PATHINFO_FILENAME);
                  // Get just Extention
                   $extention = $request->file('photo')->getClientOriginalExtension();
                  // Filename to store
                   $filenameToStore = $filename.'_'.time().'.'.$extention;
                   // Upload Image
                  $path = $request->file('photo')->storeAs('profile-photos', $filenameToStore);

       

            }

              if ($request->hasFile('photo')) {
        
              $user->profile_photo_path = $path;
       
             }

This is the View

          <!-- PROFILE INFO -->
           <form method="POST" action="{{ route('register') }}" enctype="multipart/form-data">
           @csrf
          <div x-show.transition.in="step === 1">
          <div class="mb-5 text-center">
           <div class="mx-auto w-32 h-32 mb-2 border rounded-full relative bg-gray-100 mb-4 
           shadow-inset">
            <img id="image" class="object-cover w-full h-32 rounded-full" :src="image" />
           </div>

            <label
             for="fileInput"
             type="button"
             class="cursor-pointer inine-flex justify-between items-center focus:outline-none 
             border py-2 px-4 rounded-lg shadow-sm text-left text-gray-600 bg-white hover:bg- 
             gray-100 font-medium"
              >
             <svg xmlns="http://www.w3.org/2000/svg" class="inline-flex flex-shrink-0 w-6 h-6 - 
              mt-1 mr-1" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" 
             stroke-linecap="round" stroke-linejoin="round">
             <rect x="0" y="0" width="24" height="24" stroke="none"></rect>
             <path d="M5 7h1a2 2 0 0 0 2 -2a1 1 0 0 1 1 -1h6a1 1 0 0 1 1 1a2 2 0 0 0 2 2h1a2 
              2 0 0 1 2 2v9a2 2 0 0 1 -2 2h-14a2 2 0 0 1 -2 -2v-9a2 2 0 0 1 2 -2" />
            <circle cx="12" cy="13" r="3" />
             </svg>
             Browse Photo
           </label>

          <div class="mx-auto w-48 text-gray-500 text-xs text-center mt-1">Click to add 
           profile picture</div>

            <input name="photo" id="fileInput" accept="image/*" class="hidden" type="file" 
           @change="let file = document.getElementById('fileInput').files[0];
             var reader = new FileReader();
             reader.onload = (e) => image = e.target.result;
            reader.readAsDataURL(file);">
          </div>
0 likes
17 replies
Sinnbeck's avatar

User variable isnt created before you use it

$user = \Auth::user(); //just an example.. Use the correct user here
$user->profile_photo_path = $path;
1 like
Adams_'s avatar

@Sinnbeck Thank you for that, I tried that still getting the same error, and that is because I am creating a new user and I am trying to add the profile path to the user before the user is created, but any idea how I can upload the file path while creating the user?

Sinnbeck's avatar

@Adamcy_Cooler You need to set the variable.. not create the user.

Another example

$user = new User;
$user->profile_photo_path = $path;
Adams_'s avatar

@Sinnbeck I did set variable , now I am getting this in the database C:\wamp64\tmp\php5C69.tmp

Sinnbeck's avatar

@Adamcy_Cooler So the original error is gone. Did you mean to do this perhaps?

$user = new User;
$user->profile_photo_path =  $filenameToStore;
Adams_'s avatar

@Sinnbeck Yes the original error is gone, Yes this works

       $user = new User;
         $user->profile_photo_path =  $filenameToStore;

but only the image name is stored in the database, but I will like to store including the path to the image like profile-photos/image-name.jpg

Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

@Adamcy_Cooler Then just do this

$path = 'profile-photos';
$request->file('photo')->storeAs($path,  $filenameToStore);
$user = new User;
$user->profile_photo_path =  $path . DIRECTORY_SEPARATOR .  $filenameToStore;
1 like
Adams_'s avatar

@Sinnbeck getting syntax error unexpected ' ,' on this line

      $path = 'profile-photos', $filenameToStore;
Adams_'s avatar

@Sinnbeck I tried it but I get a different error now

"Use of undefined constant DIRECTORY_SEPERATOR - assumed 'DIRECTORY_SEPERATOR' (this will throw an Error in a future version of PHP)"

so I just decided to try the update profile photo method that comes with Jetstream and it seems to be working fine, I feel like that's cheating....

     if (isset($input['photo'])) {
        $user->updateProfilePhoto($input['photo']);
    }
Snapey's avatar

check the spelling DIRECTORY_SEPARATOR

1 like

Please or to participate in this conversation.