Reputation: 144
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?
Upvotes: 0
Views: 787
Reputation: 1062
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.
Upvotes: 0
Reputation: 835
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.
Upvotes: 2