简体   繁体   中英

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

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

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

User.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

在此处输入图像描述

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

在此处输入图像描述

If I inspect the image the src is 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.

Now, in 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:

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

To get accessible url for public URLs. Laravel provides asset helper for the same.

For example if your image is stored in

public/storage/image/user.png

You can easily access the image like below:

asset('storage/image/user.png') //outputs complete url to the file

Note: public folder is considered as base directory.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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