skip to Main Content

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:

enter image description here

and the location which is shown on the browser

enter image description here

how to solve this issue

2

Answers


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

    Login or Signup to reply.
  2. In laravel the file directly uploaded under the storage folder so for
    using it you have to run

    php artisan storage:link
    

    Check the documentation here Laravel

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search