Laravel 8 Jetstream 个人资料照片与社交名媛头像



我正在尝试Laravel 8 Jetstream与Laravel Socialite。 问题是当我检索头像并使用其 URL 进行profile_photo_path时。刀片文件将追加导致头像不显示的 http://localhost:8000/storage/。 截图在这里

我已启用 jetstream 的个人资料照片配置/喷射流.php

'features' => [
Features::profilePhotos(),
Features::accountDeletion(),
],

以下是我存储用户app/Http/Controllers/LoginController的方式.php

protected function registerOrLoginUser($user, $driver=null)
{
if(!is_null($driver)){
$whereColumn = null;
if($driver == 'facebook'){
$whereColumn = "facebook_id";
}
if($driver == 'google'){
$whereColumn = "google_id";
}
try{
$findUser = User::where($whereColumn, $user->id)->first();
if($findUser){
Auth::login($findUser);
} else{
$newUser = new User();
$newUser->name = $user->name;
$newUser->email = $user->email;
$newUser->password = encrypt('');
$newUser->$whereColumn = $user->id;
$newUser->profile_photo_path = $user->getAvatar();
$newUser->save();
Auth::login($newUser);
}

我在这里看到了问题 资源/视图/导航菜单.刀片.php

<button class="flex text-sm border-2 border-transparent rounded-full focus:outline-none focus:border-gray-300 transition duration-150 ease-in-out">
<img class="h-8 w-8 rounded-full object-cover" src="{{ Auth::user()->profile_photo_url }}" alt="{{ Auth::user()->name }}" />
</button>

所以我试图补救这个刀片文件并在这里放一个 if 语句:

<button class="flex text-sm border-2 border-transparent rounded-full focus:outline-none focus:border-gray-300 transition duration-150 ease-in-out">
<img class="h-8 w-8 rounded-full object-cover" src="{{ (Auth::user()->google_id !== null ||  Auth::user()->facebook_id !== null ) ? Auth::user()->profile_photo_path : Auth::user()->profile_photo_url }}" alt="{{ Auth::user()->name }}" />
</button>

"如果声明"有缺陷,当社交名流登录用户决定改变他们的图片时,它就会中断。我尝试了这个解决方案,但它只将数值存储到我的profile_photo_path。 知道如何让社交名流和 Jetstream 的内置个人资料照片工作吗?

我从vendor/laravel/jetstream/src/HasProfilePhoto.php复制了getProfilePhotoUrlAtrribute方法AppModelsUser并修改它以接受来自社交网站的头像 URL 或检索上传的照片。

<?php
public function getProfilePhotoUrlAttribute()
{
$path = $this->profile_photo_path;
if (Storage::disk($this->profilePhotoDisk())->exists($path)){
return Storage::disk($this->profilePhotoDisk())->url($this->profile_photo_path);
} 
elseif (!empty($path)){
// Use Photo URL from Social sites link... 
return $path;
}
else {
//empty path. Use defaultProfilePhotoUrl
return $this->defaultProfilePhotoUrl();
}
}
?>

杰茨雷姆文档参考

@user2645113的答案改编为 2023 年,Laravel 10。源文件和目标文件相同,但现在函数名称为profilePhotoUrl

我确定这段代码可以更好地重构,但这是一个工作版本:

/**
* Get the URL to the user's profile photo.
*
* @return IlluminateDatabaseEloquentCastsAttribute
*/
public function profilePhotoUrl(): Attribute
{
return Attribute::get(function () {
$path = $this->profile_photo_path;
if ($path != null && Storage::disk($this->profilePhotoDisk())->exists($path)) {
return Storage::disk($this->profilePhotoDisk())->url($this->profile_photo_path);
} elseif ($path != null && !empty($path)) {
// Use Photo URL from Social sites link... 
return $path;
} else {
//empty path. Use defaultProfilePhotoUrl
return $this->defaultProfilePhotoUrl();
}
});
}

显然,请务必包括:

use IlluminateDatabaseEloquentCastsAttribute;
use IlluminateSupportFacadesStorage;

最新更新