I am using this way to get the image url but it is not working. Can anyone help?
<img src="{{ $user->getImageUrl() }}" alt="profile picture">
I am storing the image path in the database like this
profile/xa6OUndH8AoSZ6bL1JbQe1CXM5tedXfoRbMDVgCQ.png
My function in the User
model:
function getImageUrl(){
if($this->image){
$imagePath = 'public/' . $this->image;
if(Storage::exists($imagePath)){
return url('storage/' . $this->image);
}
}
return url('storage/default-user.png');
}
Here is the location of my image:
and the location which is shown on the browser
how to solve this issue
2
Answers
Make sure to run
php artisan storage:link
https://laravel.com/docs/10.x/filesystem#the-public-disk
Also, you could leave out the
url()
function and just return a string starting with "storage/". In this case you will have to insert the URL with{!! $user->getImageURL() !!}
so it doesn’t get html encoded.If the problem is with the url() function, try making sure the APP_URL is correct in your .env file.
Check the documentation here Laravel