Laravel 8 Jetstream 个人资料照片和 Socialite 头像
Laravel 8 Jetstream Profile Photos with Socialite Avatars
我正在尝试 Laravel 8 Jetstream 和 Laravel Socialite。
问题是当我检索头像并将其 URL 用于 profile_photo_path 时。 blade 文件附加 http://localhost:8000/storage/ 导致头像不显示。
screenshot here
我已启用 jetstream 的个人资料照片
config/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);
}
我在这里看到了问题resources/views/navigation-menu.blade.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>
所以我试图修复这个 blade 文件并在此处放置一个 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>
'If statement' 有缺陷,当 socialite logged-in 用户决定更改他们的图片时它会崩溃。我试过这个 但它只将数值存储到我的 profile_photo_path。知道如何制作 socialite 和 Jetstream 的 built-in 个人资料照片吗?
我从 vendor/laravel/jetstream/src/HasProfilePhoto.php
复制了 getProfilePhotoUrlAtrribute
方法
App\Models\User
并将其修改为接受来自社交网站的头像 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();
}
}
?>
我正在尝试 Laravel 8 Jetstream 和 Laravel Socialite。 问题是当我检索头像并将其 URL 用于 profile_photo_path 时。 blade 文件附加 http://localhost:8000/storage/ 导致头像不显示。 screenshot here
我已启用 jetstream 的个人资料照片 config/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);
}
我在这里看到了问题resources/views/navigation-menu.blade.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>
所以我试图修复这个 blade 文件并在此处放置一个 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>
'If statement' 有缺陷,当 socialite logged-in 用户决定更改他们的图片时它会崩溃。我试过这个
我从 vendor/laravel/jetstream/src/HasProfilePhoto.php
复制了 getProfilePhotoUrlAtrribute
方法
App\Models\User
并将其修改为接受来自社交网站的头像 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();
}
}
?>