繁体   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