List and display images in laravel 5.3 storage

2

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

Note: Image names are saved in the database.

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

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

Thank you in advance

    
asked by anonymous 15.03.2017 / 03:33

1 answer

1

I've done something like this:

  • In the controller, responsible for the view of the images, I retrieve the database from the paths of all the images that I want to display.
  • I send this array to the view.
  • In the view, for each element of the array, I create an img tag with the path given.
  • In terms of code ...

    No controller

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

    Returning

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

    In the view

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

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

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

        
    15.03.2017 / 04:25