簡體   English   中英

如何顯示保存在刀片存儲文件夾中的圖像

[英]How to display Image saved in storage folder on blade

我想在刀片視圖中顯示保存在存儲目錄中的圖像。 我成功地將文件存儲到 storage/app/public 文件夾,就像 ProfilesController 文件一樣:

`if(request('image')) {
            $imagePath = request('image')->store('profile', 'public');

            $image = Image::make(public_path("storage/{$imagePath}"))->fit(1000, 1000);
            $image->save();
        }

        auth()->user()->profile->update(array_merge(
            $data,
            ['image' => $imagePath] 
        ));`

現在我想在我的 index.blade.php 文件中檢索圖像。 這是如何做到的:

<img src="/storage/{{$user->profile->image}}" alt="">

laravel 文檔說在我的終端中使用存儲鏈接我這樣做了

php artisan storage:link

我嘗試了所有這些,但是沒有圖像加載到視圖中!

我做錯了什么以及如何解決!

    <img class="" src="{{ Storage::disk('name-option')->url('full path.jpg') }}"  alt="">

disk('name-option') 是您可以跳過的選項

<img class="" src="{{ Storage::url('full path.jpg') }}"  alt="">

您應該在用戶配置文件模型中使用訪問器,即:-

public function getImageAttribute($image) { return asset('storage'.$image); }

這就是我要解決的方法。 實際的解決方案是通過 storage:link 命令將存儲目錄與公用文件夾鏈接起來,按照文檔在此處輸入鏈接描述

if(request('image')) {
       $imageName=time().'.' . $request->file('image')->extension();

        $imagePath = request('image')->storeAs('profile', $imageName);

        //you can use $imagePath to save the name as well as the path of the image
        $image->save();
    }

    ;`

在 config 文件夾中的 filesystems.php 文件中,配置文件夾 append 與 public 文件夾匹配,如下所示:

'links' => [
    public_path('storage') => storage_path('app/public'),
    public_path('profile') => storage_path('app/profile'),

],

確保重新運行命令:

php artisan storage:link

為了訪問圖像,

<img src="{{asset($user->profile->image)}}" alt="">

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM