0
votes

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

enter image description here

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

enter image description here

If I inspect the image the src is src="http://127.0.0.1:8000/images/dT9gvraL3HbTlHcQly96YwoSJdikNj7fVL1dsIzT.jpeg". How did I do wrong?

2
How are you storing image link in db? Is it with url or just name of image?Abhishek Honrao

2 Answers

2
votes

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.

0
votes

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.