簡體   English   中英

未從 Laravel 中的公用文件夾(https://localhost/storage)獲取圖像

[英]Not fetching the image from public folder( https://localhost/storage) in Laravel

我正在創建一個 web 應用程序。 當我上傳圖像時,它無法從公用文件夾中獲取並返回 404 錯誤。當我檢查錯誤時,它無法訪問http://localhost/storage並通過我一個錯誤。 我附上了錯誤截圖,請檢查並幫助我。 查看代碼。 簡介 Model:

class Profile extends Model implements HasMedia
{
    use HasMediaTrait;

    public function user()
    {
        return $this->belongsTo(User::class);
    }
public function image()
{
    if($this->media->first())
    {


        return $this->media->first()->getFullUrl('thumb');


    }


    return null;
}

    public function registerMediaConversions(?Media $media = null)
    {
        $this->addMediaConversion('thumb')
        ->width(100)
        ->height(100);
    }
}

配置文件控制器

public function update(Request $request, Profile $profile)
{
    if ($request->hasFile('image'))
    {
        $profile->clearMediaCollection('images');

        $profile->addMediaFromRequest('image')
                 ->toMediaCollection('images');

    }
    return redirect()->back();
}

Show.blade.php

@method('PATCH')
                        <div class="form-group row justify-content-center">
                            <div class='profile-avatar'>
                                <div onclick="document.getElementById('image').click()" class="profile-avatar-overlay">
                                    <svg version="1.1" xmlns="http://www.w3.org/2000/svg" width="100" height="100" viewBox="0 0 32 32">

                                        <path d="M18 22.082v-1.649c2.203-1.241 4-4.337 4-7.432 0-4.971 0-9-6-9s-6 4.029-6 9c0 3.096 1.797 6.191 4 7.432v1.649c-6.784 0.555-12 3.888-12 7.918h28c0-4.030-5.216-7.364-12-7.918z"></path>
                                        </svg>

                                </div>
                                <img src="{{ $profile->image() }}" alt="" >

                            </div>
                        </div>

錯誤圖片: 在此處輸入圖像描述

php artisan storage:link

這會為您創建從 public/storage 到 storage/app/public 的符號鏈接,僅此而已。 現在 /storage/app/public 中的任何文件都可以通過如下鏈接訪問:

http://somedomain.com/storage/image.jpg

像這樣設置您的圖像 src 路徑:

{{ URL::to('image/' .$profile->image()) }}

暫無
暫無

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

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