Skip to content
Advertisement

how to retrieve images or files from a certain route page in laravel?

I am having issues displaying or retrieving user profile photo at certain routes. it works perfectly for other views especially that main views. photos fails to appear when i try to display on a certain view but make changes to the route.

example: 1 if i try displaying or retrieving images from my database with this view and route, it will fail

Route::get('staff/staffprofile/payslip', function () {
    return view('payslip');
});

example 2: works using this:

Route::get('/payslip', function () {
    return view('payslip');
});

This is how i display the photo and it works for Example 2

<img src="uploads/{{$stp->image}}">

i need assist on how to display on certain routes

Advertisement

Answer

The reason it does not work is the way “pretty URLs” and paths work (relative/absolute).

Try to see your URLs with different depth “levels”:

  • /staff/staffprofile/payslip Would be level 2
  • /payslip would be level 0

Given the fact that you probably use a “public” folder where you store all your assets and your index.php file, you need to ensure that the webserver will actually use a path relative to your “root” folder, in this case the public folder.


Say your images are stored in .../public/uploads/. (The ... are the previous folders in the path)

Depending on how you link your image, the web server will search for the image in a different location:

Without the first forward slash (uploads/image.png)

  • On /staff/staffprofile/payslip : .../public/staffprofile/payslip/uploads/image.png
  • On /payslip : .../public/uploads/image.png

Without forward slash, it only works on “level 0” URLs.

Whereas, with a forward slash (/uploads/image.png)

  • On /staff/staffprofile/payslip : .../public/uploads/image.png
  • On /payslip : .../public/uploads/image.png

I have also learnt it the hard way and figured it out randomly, so I’m glad it helped someone.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement