Image not displaying in view. Laravel
Asked Answered
U

7

12

In my Laravel project I'm trying to load an image into my view using blade but it displays as a broken link. I inspect it with firebug and the src is pointing to the image but nothing is displaying.

My image is located in my project's public/images/users/3/profilePictures/

Here is my <img> in the view.blade.php

<img class="com-profile-picture" src="images/users/{{ $follower->id }}/profilePictures/50thumb-{{ $follower->profilePicture->url }}" alt="{{ $follower->first_name }}&nbsp;{{ $follower->last_name }} Profile" width="50" height="50">

However I get this when I load the page:

enter image description here

When I inspect the image with firebug I see this:

enter image description here

That is the correct src and the image does exist in my public/images/users/3/profilePictures/ directory

Anyone know why this is happening?

Unsociable answered 18/1, 2015 at 5:29 Comment(2)
src of firebug html & code different, html says, ../users/3.., but u say correct src is ../users/2..Surveillance
ah, miss type on my question, I'm still experiencing the same problem.Unsociable
Y
17

This may be caused you are in a route which does not represent the base URL. You should generate the URL for your assets relative to the public/ folder. Use URL::asset('path/to/asset') to generate the URL.

{{ URL::asset("images/users/{$follower->id}/profilePictures/50thumb-{$follower->profilePicture->url}") }}

Or as @lukasgeiter mentioned you can simply use asset('path/to/asset') helper.

Ytterbite answered 18/1, 2015 at 7:42 Comment(2)
Thank you very much @Ytterbite and lukasgeiterUnsociable
This is just save my life :DAllophane
L
18

You can try with the

php artisan storage:link

else you can use this

A simple fix for the issue (linked broken error) in .env file

APP_URL=http://localhost

this replace with the

APP_URL=http://localhost:8000

then the issue will be fixed.

Laveralavergne answered 17/9, 2019 at 20:55 Comment(1)
Sometimes when you run more than one project and localhost:8000 is already in use, remember that if you use localhost:8001 your images might not be visible. You will have to change APP_URL=localhost:8000 to APP_URL=localhost:8001Mongoose
Y
17

This may be caused you are in a route which does not represent the base URL. You should generate the URL for your assets relative to the public/ folder. Use URL::asset('path/to/asset') to generate the URL.

{{ URL::asset("images/users/{$follower->id}/profilePictures/50thumb-{$follower->profilePicture->url}") }}

Or as @lukasgeiter mentioned you can simply use asset('path/to/asset') helper.

Ytterbite answered 18/1, 2015 at 7:42 Comment(2)
Thank you very much @Ytterbite and lukasgeiterUnsociable
This is just save my life :DAllophane
T
3

delete already generated symlink and then run this command

php artisan storage:link
Thermonuclear answered 18/6, 2021 at 9:5 Comment(0)
S
1

This is a very old question but here is the answer for new developers who might face the same issue

  1. Delete the existing storage symilnk by simply deleting the "storage" link from the public folder,
  2. check for the APP_URL variable in .env (it should be exactly the same as your current project URL including port i.e APP_URL=HTTP://localhost:8000
  3. Run the artisan command "php artisan storage:link"

This must resolve your issue, if it doesn't please message me I'll help you debug it.

Sweetener answered 10/4 at 4:24 Comment(0)
M
0

if you are using laravel, consider excluding public folder or any folder where your public files are store in .htacccess from folders that laravel control.

Do it like this in .htaccess

# Exclude directory from rewriting RewriteRule ^(public/) - [L]

Mafaldamafeking answered 15/2, 2022 at 12:43 Comment(0)
N
0

Storage location before (Not Working) :

<img src="{{asset('storage/app/public/media/productImages/'.$list->image)}}" alt="" srcset=""> 

Working storage location :

<img src="{{asset('storage/media/productImages/'.$list->image)}}" alt="" srcset="">

Else try :

  1. Deleting all the linked/shortcut folders created in public folder
  2. Then recreating the link by running :

php artisan storage:link

Napoli answered 27/7, 2022 at 15:57 Comment(0)
S
-1

Goto public Folder

Delete the storage shortcut Then create Another shortcut for storage Folder Create Shortcut Storage Folder Then run php artisan storage:link

Sholley answered 11/3, 2023 at 10:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.