Mohamed Hamdy
Mohamed Hamdy

Reputation: 55

how to display multiple images from database to blade view, by Laravel 5.3?

I have a multiple images in "Images" column in DB, and I need to display all Images in:

@foreach(explode(',', $posts->images) as $images)
<div class="col-lg-2 col-md-2 col-sm-2">
    <a href="{{ URL::to('img/offers/'.$images)}}"
       class="portfolio-box">
        <img src="{{ URL::to('img/offers/'.$images)}}"
             class="img-responsive" alt="--">
        <div class="portfolio-box-caption">
            <div class="portfolio-box-caption-content">
            <span class="glyphicon glyphicon-zoom-in"
                  style="font-size: 80px"></span>
            </div>
        </div>
    </a>
</div> 
@endforeach

but it not working because link show like:

http://localhost:8000/img/offers/["149939592986Dynamic-Web.png"

and next image show like:

http://localhost:8000/img/offers/"149938949479Static-Web.png"]

now, what i do to display all images ?

my column in BD show like an array:

["149938949418Dynamic-Web.png","149938949479Static-Web.png"]

thank you.

Upvotes: 0

Views: 6886

Answers (2)

Chris
Chris

Reputation: 3461

The reason your example does not work is because you are exploding a json string. What you will have to do is to simple decode it properly, like this:

@foreach(json_decode($posts->images, true) as $images)
<div class="col-lg-2 col-md-2 col-sm-2">
    <a href="{{ URL::to('img/offers/'.$images)}}"
       class="portfolio-box">
        <img src="{{ URL::to('img/offers/'.$images)}}"
             class="img-responsive" alt="--">
        <div class="portfolio-box-caption">
            <div class="portfolio-box-caption-content">
            <span class="glyphicon glyphicon-zoom-in"
                  style="font-size: 80px"></span>
            </div>
        </div>
    </a>
</div> 
@endforeach

This will loop through all your images and display the correct strings like:

http://localhost:8000/img/offers/149939592986Dynamic-Web.png

Upvotes: 3

Sagar Gautam
Sagar Gautam

Reputation: 9369

You have json encoded data so you have to decode. So, you can use json_decode() to extract array of image file names as:

 $images = json_decode($posts->images,true);

After this you can use simple for loop to show all images,

 @foreach($images as $image)
     <div>
          <--! Write code to display image -->
     </div>
 @endforeach

Upvotes: 0

Related Questions