[英]Laravel 8 Jetstream profile-photos not showing
Laravel 8、jetstream,真的是新的嗎?
我只是嘗試在 fortify 中安裝和播放它,但我真的不明白為什么我的個人資料照片沒有顯示圖片。
更新配置文件信息表
<!-- Current Profile Photo -->
<div class="mt-2" x-show="! photoPreview">
<img src="{{ $this->user->profile_photo_url }}"
alt="{{ $this->user->name }}"
class="rounded-full h-20 w-20 object-cover">
</div>
.環境
APP_NAME=Laravel
APP_ENV=local
APP_KEY=base64:6IJbdi+QYczKeLT7yOw3OgPsHucXn1KxVUb27hTQKpU=
APP_DEBUG=true
APP_URL=http://127.0.0.1:8000
配置/文件系統
'public' => [
'driver' => 'local',
'root' => storage_path('/public/storage'),
'url' => env('APP_URL').'/public/storage/',
'visibility' => 'public',
],
您的問題是.env文件中的默認 APP_URL 設置:
APP_NAME=Laravel
APP_ENV=local
APP_KEY=base64:6IJbdi+QYczKeLT7yOw3OgPsHucXn1KxVUb27hTQKpU=
APP_DEBUG=true
APP_URL=http://localhost
artisan serve 命令在http://127.0.0.1:8000上啟動了該應用程序,因此您相應地更改了 APP_URL:
APP_URL=http://127.0.0.1:8000
我不會推薦這個。 有時,出於各種原因,該命令可能會將端口更改為 8001、8002 等。
$ php artisan serve
Starting Laravel development server: http://127.0.0.1:8000
Failed to listen on 127.0.0.1:8000 (reason: Address already in use)
Starting Laravel development server: http://127.0.0.1:8001
PHP 7.4.12 Development Server (http://127.0.0.1:8001) started
只需評論或將.env文件中的APP_URL留空:
#APP_URL=http://localhost
APP_URL=
這將從配置文件圖像中刪除 http://localhost 部分並解決問題:
<img src="/storage/profile-photos/photo.jpeg">
無論如何,您將在生產環境中擁有不同的.env文件。
您在 config/filesystems.php 文件中做了一個小改動:
'public' => [
'driver' => 'local',
'root' => storage_path('/public/storage'),
'url' => env('APP_URL').'/public/storage/',
'visibility' => 'public',
],
原來的設置是:
'root' => storage_path('/app/public'),
它僅在您刪除並重建符號鏈接后才起作用。
但是現在,在您的項目目錄中,您可能會遇到這樣的情況:
- public - storage + css - app + js - public - storage - profile-photos - public - public - storage - storage - profile-photos - profile-photos .htaccess + framework favicon.ico + logs index.php mix-manifest.json robots.txt web.config
基本上,您制作了一個新磁盤。 我想這不是故意的。
此外,您的個人資料照片的網址中可能有一些令人困惑的部分:
.../storage/public/storage/profile-photos/... .
您可以恢復到原始設置,刪除附加文件夾,並重建系統鏈接。
配置/文件系統.php
'disks' => [
//...
'public' => [
'driver' => 'local',
'root' => storage_path('app/public'),
'url' => env('APP_URL').'/storage',
'visibility' => 'public',
],
//...
],
您需要先在 public 下創建存儲然后鏈接它。 所以首先去laravel項目主文件夾,然后是公共文件夾。如果存儲鏈接不存在,則創建它。 然后回到項目主文件夾並創建存儲鏈接
$ cd (laravel project folder)
$ cd public
$ rm -r storage
$ cd ..
$ php artisan storage:link
它應該解決問題..
在 .env 中設置你的 APP_URL ,我的是 APP_URL=http://localhost:8000 並在 CLI 中運行命令作為
php artisan storage:link
為 storage/app/public 創建符號鏈接,如果您尚未運行該命令,您將不會在儀表板中獲得個人資料圖像。
個人資料圖片是否存儲成功?
(窗口)如果是,這些是我修復它的步驟:
在名為jetstream.php的配置文件夾中找到文件,並在返回的數組中查找“features”鍵並取消對Features::profilePhotos()值的注釋。
前:
'features' => [
// Features::profilePhotos(),
// Features::api(),
// Features::teams(),
],
后:
'features' => [
Features::profilePhotos(),
// Features::api(),
// Features::teams(),
],
我這樣做了:在 filesystems.php 上刪除 (APP_URL),這解決了我的問題,個人資料圖片未顯示在 jetstream 上
'public' => [
'driver' => 'local',
'root' => storage_path('app/public'),
'url' => env('').'/storage',
'visibility' => 'public',
],
抱歉,我遇到了這個級別的問題
在名為 jetstream.php 的配置文件夾中找到文件並在返回的數組中查找“features”鍵並取消對Features::profilePhotos()
注釋
轉到 .env 並將應用程序 uri 更改為此APP_URL=http://localhost:8000
在終端輸入:
php artisan serve
我在使用 Laravel 8(當前最新版本)處理我的項目時遇到了同樣的問題,以下是我解決這個問題的兩種方法:
在第一種方法中,您可以輕松地將瀏覽器中的應用程序 URL 從 127.0.0.1:8000 更改為localhost:8000 ,然后使用命令php artisan storage:link將 public/storage 與 storage/app/public 文件夾鏈接起來
或者在第二種方法中,您可以編輯環境文件。 轉到環境配置文件 (.env) 並將 APP_URL 從 http://localhost 更改為http://127.0.0.1:8000 ,然后使用命令php artisan storage將 public/storage 與 storage/app/public 文件夾鏈接:關聯
最后,使用命令php artisan serve重新啟動應用程序,現在它必須工作了。 我希望它可以幫助任何面臨此類問題的人。
在 config/app.php 中,APP_URL 可能需要一個端口,如:
'url' => env('APP_URL', 'http://localhost:8080'),
確保您的 .env APP_URL 正確並確保運行 php artisan storage:link。 https://github.com/laravel/framework/issues/34319#issuecomment-691677346
'public' => [
'driver' => 'local',
'root' => storage_path('app/public'),
'url' => '/storage',
'visibility' => 'public',
],
遠足
我遇到了同樣的問題,只需運行php artisan storage:link就可以了。
注意:我在 laravel 的新啟動時運行此命令並且沒有更改我的存儲路徑設置(默認)
如果您的 Jetstream 個人資料照片未顯示,您必須執行簡單的 3 個主要步驟
首先,我們假設您已經在 profilePhoto 的 config/jetstream 功能中啟用
將您的 .env 文件更改為 artisan serve 鏈接已經啟動。 像這樣
APP_URL=http://127.0.0.1:8000
他們執行這些命令
重新加載或再次上傳您的照片個人資料並完成
我知道這里還有很多其他答案,這是一個比較老的問題,但由於它在谷歌搜索中排名很高,我將添加我發現的問題,希望它能幫助面臨類似問題的人。
我能夠看到我的個人資料照片已上傳,並且此處的所有設置和建議似乎都是正確的,但是當我嘗試訪問我的模板中的 user.profile_photo_url 時,我仍然沒有收到任何 output。 事實證明,在 HandleInertiaRequests.php 中,我共享了用戶 object,但只允許它顯示某些屬性。 我只需要將“profile_photo_url”添加到屬性列表中,它看起來像這樣:
public function share(Request $request)
{
return array_merge(parent::share($request), [
'ziggy' => function () use ($request) {
return array_merge((new Ziggy)->toArray(), [
'location' => $request->url(),
]);
},
'user' => fn () => $request->user()
? $request->user()->only('id', 'name', 'email', 'email_verified_at', 'current_team_id', 'account_id', 'profile_photo_url')
: null,
]);
}
在我的例子中,幾個月前我聽從了一些人的建議,將默認的 ui-avatars 更改為 Gravatar,我的用戶 model 正在實現 hasProfilePhoto 特征的功能......現在如果你不是這種情況,我建議將相關功能實現到您的用戶 model,特別是因為 JetStream 不是可更新的供應商 package,而是您的應用程序的引導起點。 至少你可以玩這些功能輸出並找出問題所在。 看來主要問題是照片上傳了,但沒有通過下面的function檢索,你可以在用戶model中實現,看看output的哪一部分是不正確的:
public function getProfilePhotoUrlAttribute()
{
return $this->profile_photo_path
? Storage::disk($this->profilePhotoDisk())->url($this->profile_photo_path)
: $this->defaultProfilePhotoUrl();
}
我希望這有幫助。
我也這樣做了:從 config/filesystems.php 中刪除了 (APP_URL)
'public' => [
'driver' => 'local',
'root' => storage_path('app/public'),
'url' => env('').'/storage',
'visibility' => 'public',
],
但這對我來說只解決了一半的問題。 該圖像現在僅顯示在導航欄中,但仍不顯示在個人資料頁面中。 為此,我這樣做了:在 update-profile-information.blade.php 文件中,我將圖像源更改為
/storage/profile-photos/{{basename($this->user->profile_photo_path)}}
(可能不是一個合適的解決方案,但它現在對我的學習項目有用)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.