artisticre's avatar

Upload Image in UpdateUserProfileInformation.php

I am getting undefined variable request when trying to add an upload function to UpdateUserProfileInformation.php Laravel Fortify.

class UpdateUserProfileInformation implements UpdatesUserProfileInformation
{
    /**
     * Validate and update the given user's profile information.
     *
     * @param  mixed  $user
     * @param  array  $input
     * @return void
     */
    public function update($user, array $input)
    {
        Validator::make($input, [
            'name' => ['required', 'string', 'max:255'],
            'email' => [
                'required',
                'string',
                'email',
                'max:255',
            'address'=>['required','string','max:255'],
            'city' => ['required', 'string', 'max:255'],
            'state' => ['required', 'string', 'max:255'],
            'zipcode' => ['required', 'string', 'max:255'],
            'homephone' => ['required', 'string', 'max:255'],
            'cellphone' => ['required', 'string', 'max:255'],
            // 'avatar' => ['mimes:jpg,jpeg,png,gif','max:4096'],
            
                Rule::unique('users')->ignore($user->id),
            ],


        ])->validateWithBag('updateProfileInformation');
        

        if ($input['email'] !== $user->email &&
            $user instanceof MustVerifyEmail) {
            $this->updateVerifiedUser($user, $input);
        } else {
            if ($request()->hasFile('avatar')) {
                $image = $request()->file('avatar');
                $filename = time() .'.'. $image->getClientOriginalExtension();
                $path = public_path('dashboard/images/profile/');
                Image::make($image)->save($path.$filename);
                if ($user->image !== 'default.png') {
                    File::delete($path.$user->avatar);
                }
              

            $user->forceFill([
            'name' => $input['name'],
            'email' => $input['email'],
            'address' => $input['address'],
            'city' => $input['city'],
            'state' => $input['state'],
            'zipcode' => $input['zipcode'],
            'homephone' => $input['homephone'],
            'cellphone' => $input['cellphone'],
            'avatar' => $user->avatar = $filename,

            
            ])->save();
            alert()->success('Profile Updated','User Profile Updated Successfully!!!');

        }
    }
    }
    /**
     * Update the given verified user's profile information.
     *
     * @param  mixed  $user
     * @param  array  $input
     * @return void
     */
    protected function updateVerifiedUser($user, array $input)
    {
        if ($request()->hasFile('avatar')) {
            $image = $request()->file('avatar');
            $filename = time() .'.'. $image->getClientOriginalExtension();
            $path = public_path('dashboard/images/profile/');
            Image::make($image)->save($path.$filename);
            if ($user->image !== 'default.png') {
                File::delete($path.$user->avatar);
            }
            $user->avatar = $filename;
       
       
        $user->forceFill([
            'name' => $input['name'],
            'email' => $input['email'],
            'address' => $input['address'],
            'city' => $input['city'],
            'state' => $input['state'],
            'zipcode' => $input['zipcode'],
            'homephone' => $input['homephone'],
            'cellphone' => $input['cellphone'],
            'avatar' => $user->avatar = $filename,
               
        ])->save();
     
        alert()->success('Profile Updated','User Profile Updated Successfully!!!');


        $user->sendEmailVerificationNotification();
    }
}
}
0 likes
4 replies
Sinnbeck's avatar

Here you are using request as a variable (you do it multiple places)

($request()->hasFile('avatar')) //before
(request()->hasFile('avatar')) //after 
1 like
artisticre's avatar

In this same post, This code is supposed to update the avatar without deleting avatar.png. It updates the avatar image but deletes avatar.png. Shouldn't this keep it from deleting?

 if (request()->hasFile('avatar')) {
                $image = request()->file('avatar');
                $filename = time() .'.'. $image->getClientOriginalExtension();
                $path = public_path('dashboard/assets/images/profile/');
                Image::make($image)->save($path.$filename);
                if ($user->image !== 'avatar.png') {
                    File::delete($path.$user->avatar);
                }
Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

@artisticre not really related to the original issue. But perhaps your column is avatar, not image?

if ($user->image !== 'avatar.png') {
if ($user->avatar !== 'avatar.png') {

Please or to participate in this conversation.