colehendricks@live.com's avatar

How To Grab User ID From Auth New User Object

I have a situation where I am using the auth methods that come with Laravel and I need to grab the User ID of the newly created user. I need the user ID because I want to set their uploaded avatar file name to be the user id so when it gets updated it just overwrites the previous file. This will reduce clutter so that the user only ever has 1 avatar file associated with them.

As you will see below, everything is working but I need help grabbing the user ID from the returned User::create object/method. Once I have that I can properly set the save path for the avatar file and for the database image path record.

If anyone can help me with this, I would greatly appreciate it.

protected function create(array $data)
    {

        return User::create([
            'email' => $data['email'],
            'first_name' => $data['first_name'],
            'last_name' => $data['last_name'],
            'display_name' => $data['display_name'],
            'title' => $data['title'],
            'location' => $data['location'],
            'work_phone' => $data['work_phone'],
            'cell_phone' => $data['cell_phone'],
            'skype' => $data['skype'],
            'twitter' => $data['twitter'],
            'password' => bcrypt($data['password']),
        ]);

        Image::make($data['profile_img']->getRealPath())->resize(128,128)->save(public_path('storage/avatars/' . /* User ID here */ . $data['profile_img']->extension()));


        // Set User profile_img column in database as the user id .upload-extension
    }
0 likes
4 replies
tomopongrac's avatar

You can try with

protected function create(array $data)
    {

        $user = User::create([
            'email' => $data['email'],
            'first_name' => $data['first_name'],
            'last_name' => $data['last_name'],
            'display_name' => $data['display_name'],
            'title' => $data['title'],
            'location' => $data['location'],
            'work_phone' => $data['work_phone'],
            'cell_phone' => $data['cell_phone'],
            'skype' => $data['skype'],
            'twitter' => $data['twitter'],
            'password' => bcrypt($data['password']),
        ]);

    // $user->id <--this is id

        Image::make($data['profile_img']->getRealPath())->resize(128,128)->save(public_path('storage/avatars/' . /* User ID here */ . $data['profile_img']->extension()));


        // Set User profile_img column in database as the user id .upload-extension
    }
colehendricks@live.com's avatar

@tomi

I tried that but got this error.

Type error: Argument 1 passed to Illuminate\Auth\SessionGuard::login() must implement interface Illuminate\Contracts\Auth\Authenticatable, null given

It created the user in the database but I am guessing it doesn't properly pass that user through to automatically login after registering.

Appreciate you trying to help with this.

tomopongrac's avatar
Level 51

can you post the code ...

maybe you can try on end of method return user

protected function create(array $data)
    {

        $user = User::create([
            'email' => $data['email'],
            'first_name' => $data['first_name'],
            'last_name' => $data['last_name'],
            'display_name' => $data['display_name'],
            'title' => $data['title'],
            'location' => $data['location'],
            'work_phone' => $data['work_phone'],
            'cell_phone' => $data['cell_phone'],
            'skype' => $data['skype'],
            'twitter' => $data['twitter'],
            'password' => bcrypt($data['password']),
        ]);

    // $user->id <--this is id

        Image::make($data['profile_img']->getRealPath())->resize(128,128)->save(public_path('storage/avatars/' . /* User ID here */ . $data['profile_img']->extension()));


        // Set User profile_img column in database as the user id .upload-extension

    return $user; // <-- add this line
    }
colehendricks@live.com's avatar

Thanks @tomi

That solution worked. I am not sure if I did the profile_img updating correctly but it appears to be saving correctly in the database. Wasn't sure if there was a more effecient method or if I should be calling the save method in the same function that the User::create is happening as well.

protected function create(array $data)
    {

        $user = User::create([
            'email' => $data['email'],
            'first_name' => $data['first_name'],
            'last_name' => $data['last_name'],
            'display_name' => $data['display_name'],
            'title' => $data['title'],
            'location' => $data['location'],
            'work_phone' => $data['work_phone'],
            'cell_phone' => $data['cell_phone'],
            'skype' => $data['skype'],
            'twitter' => $data['twitter'],
            'password' => bcrypt($data['password']),
        ]);

        Image::make($data['profile_img']->getRealPath())->resize(128,128)->save(public_path('storage/avatars/' . $user->id . '.' . $data['profile_img']->extension()));


        $user->profile_img = $user->id . '.' . $data['profile_img']->extension();
        $user->save();

        return $user;
    }

Please or to participate in this conversation.