Laravel socialite保存头像 [英] Laravel socialite Save avatar

查看:70
本文介绍了Laravel socialite保存头像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每个人,我需要将图像名称保存在数据库的头像列中并将图像保存在public/Storage/users中.请在任何帮助下将图像链接保存到数据库中.

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('/');
}

推荐答案

使用 file_get_contents 函数简单地获取数据并处理检索到的数据.

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);
}

替换 $ user-> getAvatar()

使用辅助方法

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

这篇关于Laravel socialite保存头像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆