List and display images in the Laravel 5.3 Storage

Asked

Viewed 3,314 times

2

I develop an application with Laravel 5.3, where the _DocumentRoot_ stays in the folder public. Upload images are in storage/app/public, but I don’t know how to list these images in a view, I can only display them directly through the response() of Laravel.

Note: the names of the images are saved in the database.

If I use return response()->file('caminho_da_imagem') in view or in the controller it returns me the image in a dark background;

I also tried using Intervetion Image with Image::make('caminho_da_imagem')->response();, but the result was the same.

Thanks in advance

1 answer

1


I’ve done something like this:

  1. In the controller, responsible for the view of the images, I rescue from the database the paths of all the images I want to display.
  2. Send this array to the view.
  3. In the view, for each element of the array, I create an img tag with the given path.

In terms of code...

On the controller

$fotos = Imagem::where('album_id', album_id)->get()->pluck('path');

Returning

return view('nome.da.view')->with([
    'fotos' => $fotos
]);

In view

@if( count($fotos)>0 )
    @foreach( $fotos as $foto )
        <img src="{{ asset('storage/'.$foto) }}">
    @endforeach
@endif

Well, that was the solution I found and it worked. I’m just a beginner in Laravel and I’m sharing my experience. I hope I’ve helped.

Note: The code may be different for you but the logic remains the same.

  • Thanks for your attention, but I’m using the default Laravel upload folder => storage subfolders app/public. And not the briefcase public subfolder storage.

  • I inked the Torage and it worked out the way you said it. Thank you

  • Good, I had forgotten to quote the folder link. Sorry!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.