简体   繁体   English

如何在 Laravel 中显示来自 public/storage/images 的图像

[英]How to display the image from the public/storage/images in Laravel

profile.blade.php profile.blade.php

 <form action="{{route('user.profile.update',$user)}}" method="post" enctype="multipart/form-data">
            @csrf
            @method('PUT')

                <div>
                <img height="100px;" class="img-profile rounded-circle" src="{{$user->avatar}}">

                </div>
                <br>
                <div class="form-group">
                <input type="file" name="avatar">

                </div>
    </form>

web.php web.php

Route::put('admin/users/{user}/update','UserController@update')->name('user.profile.update');

User.php用户.php

public function getAvatarAttribute($value){
        return asset($value);
    }

UserController:用户控制器:

public function update(User $user){


        $inputs=request()->validate([

                'file'=>['file'],




                ]);


        if(request('avatar')){
           $inputs['avatar']=request('avatar')->store('images');
        }

        $user->update($inputs);

        return back();
    }
}

This is a profile.blade.php这是一个profile.blade.php

在此处输入图像描述

How to display the image from the public/storage/images in Laravel如何在 Laravel 中显示来自 public/storage/images 的图像

在此处输入图像描述

If I inspect the image the src is src="http://127.0.0.1:8000/images/dT9gvraL3HbTlHcQly96YwoSJdikNj7fVL1dsIzT.jpeg".如果我检查图像,src 是 src="http://127.0.0.1:8000/images/dT9gvraL3HbTlHcQly96YwoSJdikNj7fVL1dsIzT.jpeg"。 How did I do wrong?我怎么做错了?

Considering that you are storing images with name not with url.考虑到您存储的图像名称不是 url。

Now, in User.php,现在,在 User.php 中,

public function getAvatarAttribute($value){ 
    return asset('storage/'.$value); 
}

Hope this will help you.希望这会帮助你。

My bad, I skipped some parts of your question.我的错,我跳过了你问题的某些部分。 You need to change the path in User.php as following:您需要更改 User.php 中的路径,如下所示:

public function getAvatarAttribute($value){
        return asset("storage/$value");
}

To get accessible url for public URLs.为公共 URL 获取可访问的 url。 Laravel provides asset helper for the same. Laravel 为其提供了资产助手。

For example if your image is stored in例如,如果您的图像存储在

public/storage/image/user.png公共/存储/图像/用户.png

You can easily access the image like below:您可以轻松访问如下图像:

asset('storage/image/user.png') //outputs complete url to the file assets('storage/image/user.png') //输出完整的url到文件

Note: public folder is considered as base directory.注意:公用文件夹被视为基本目录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM