laravel-medialibrary: getFirstMediaUrl() doesn't retrieve the full path to the file

I am using v7 of this library, when I use the method:

echo $inmueble->getFirstMediaUrl('destacadas');

I got this: //storage/1/1-1.jpg

But a colleague is working on the same project also and the method shows him the full path:

http://localhost:8888/inmuebles/public//storage/1/1-1.jpg

Do I need to change my configuration or why I am not able to get the full path automatically?

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Comments: 30 (7 by maintainers)

Most upvoted comments

In case someone has the same error in my case was the URL defined in the env file:

You should put the URL without the final slash: Bad: APP_URL=http://my-domain.test/ Good: APP_URL=http://my-domain.test

I tried $inmueble->getFirstMedia('destacadas')->getFullUrl();

but I still got the same incomplete path: //storage/1/1-1.jpg

Laravel uses the app.url config value to generate urls. You probably need to specify APP_URL in your .env file.

i noticed that when APP_URL contains https getFirstMediaUrl return correctly full url but with there is a problem with http. Only partial url is being returned

Good news everyone, I found solution for those, who make own custom disk for media library storage. image Here is my config for media disk which i make to separate storage and media-library content. Disk must be public and you must define url parameter as required.

If you tried the above mentioned and did not get results, maybe you are making a mistake in the first place. Just like me 😃

try this: php artisan storage:link

But the most important answer is that you edit the APP_URL in .env. Later, what I said will likely end your problem.

The problem may be solved, before you can use storage in public folder change APP_URL in .env and config/App.php http://localhost change to http://localhost/project/public and run this may be ok.

This might be related to the way your storage is configured, but you’d need to dive into the LocalUrlGenerator class to find out what’s missing.

Did you try fullUrl as previously suggested? It’s a public method on Media, I’d suggest doing that if you need a full path.

https://github.com/spatie/laravel-medialibrary/blob/v7/src/Models/Media.php#L45-L51

I didn’t find the getFullUrl() method on docs, also $inmueble it’s already the model which it’s related to the media library, not an array. Thanks for your suggestion!