Jack Robson
Jack Robson

Reputation: 2302

Laravel Doesn't Render Images

I have setup a site using Laravel version 6.0.3 on my local machine.

I ran this command:

php artisan storage:link

And have uploaded an image file 'black.png' to my site.

The upload works well and the file exists relative to root at:

storage/app/posts/KiL8ZKWQIq3CK8ecd4CDVZHACFHlUsMAyq8h0AJj.png

However when I use the assets function

$image = "posts/KiL8ZKWQIq3CK8ecd4CDVZHACFHlUsMAyq8h0AJj.png";
assets($image);

It outputs the following link to the image:

http://cms.test/posts/KiL8ZKWQIq3CK8ecd4CDVZHACFHlUsMAyq8h0AJj.png

Which doesn't give a 404 error. And when visited is a blank page.

What could I be missing?

EDIT:

If I try to use:

{{ asset('storage/posts/KiL8ZKWQIq3CK8ecd4CDVZHACFHlUsMAyq8h0AJj.png') }} 

Then when I load the page I get a 404 error for the image at URL:

 http://cms.test/storage/posts/KiL8ZKWQIq3CK8ecd4CDVZHACFHlUsMAyq8h0AJj.png

Similarly, if I try:

{{ asset('storage/app/posts/KiL8ZKWQIq3CK8ecd4CDVZHACFHlUsMAyq8h0AJj.png') }} 

I also get a 404 error for the URL: http://cms.test/storage/app/posts/KiL8ZKWQIq3CK8ecd4CDVZHACFHlUsMAyq8h0AJj.png

Response in network debug window after returning to initial code:

$image = "posts/KiL8ZKWQIq3CK8ecd4CDVZHACFHlUsMAyq8h0AJj.png";
assets($image);

Shows a 200 status response code from the asset.

enter image description here

FINAL EDIT:

Following ceejayoz' instruction, I made two crucial tweaks. First inside of config/filesystems.php I changed one of the local variables:

'root' => storage_path('app/public'),

And in my template call I used:

{{ asset('storage/' . $image) }}"

And now it works.

Upvotes: 1

Views: 437

Answers (1)

ceejayoz
ceejayoz

Reputation: 180014

php artisan storage:link creates a symbolic link from public/storage to storage/app/public.

This makes anything in storage/app/public available at https://example.com/storage/. It doesn't make anything outside storage/app/public available anywhere - so the content in your storage/app/posts is still inaccessible in the browser.

If you put your post images in storage/app/public/posts, they'll be accessible by doing asset('storage/posts/foo.jpg') if you have the link set up correctly.

Upvotes: 2

Related Questions