gpt4 book ai didi

php - Laravel 8 Jetstream 个人资料照片与社交名流头像

转载 作者:行者123 更新时间:2023-12-05 02:43:38 29 4
gpt4 key购买 nike

我正在尝试 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 语句”存在缺陷,当社交名流登录用户决定更改他们的图片时,它会中断。我试过这个 solution但它只将数值存储到我的 profile_photo_path 中。知道如何让社交名流和 Jetstream 的内置个人资料照片发挥作用吗?

最佳答案

我从 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();
}
}
?>

Jetsream docs reference

关于php - Laravel 8 Jetstream 个人资料照片与社交名流头像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66817733/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com