简体   繁体   中英

Laravel socialite Save avatar

everybody, I need to Save image name in the avatar column in the database and save the image in public/Storage/users My Code Here Save image link in the database any help, please.

public function handleProviderCallback($service)
{
    $user = Socialite::driver($service)->user();
    $FindUser = User::where('email',$user->getEmail())->first();
        if($FindUser){
            Auth::login($FindUser);
        }
        else{
            $NewUser = new User;
            $NewUser->email = $user->getEmail();
            $NewUser->name  = $user->getName();
            $NewUser->avatar  = $user->getAvatar();
            $NewUser->password  = bcrypt(123456);
            $NewUser->save();
            Auth::login($NewUser);
        }
    return redirect('/');
}

Simply fetch the data using file_get_contents function and process the retrieved data.

use File; 

create a helper method to get the avatar

function getSocialAvatar($file, $path){
    $fileContents = file_get_contents($file);
    return File::put(public_path() . $path . $user->getId() . ".jpg", $fileContents);
}

replace $user->getAvatar()

with the helper method

getSocialAvatar($user->getAvatar(), 'path')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM