Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

theUnforgiven's avatar

Bootstrap modal gallery

Hi all,

I'm wanting to open a modal window when a button is clicked that will open up and show the corresponding images.

So if I click puppy image it should show one image, where as the clicking the other one should two images.

This is the piece of (messy) code I have at the minute

@foreach($galleries as $image)
                    <button data-toggle="modal" data-target=".galleryModal_{{ strtolower(str_replace(' ', '_', $image->name)) }}" class="btn btn-sm btn-info">{{ $image->name }}</button>
                    <div class="modal fade galleryModal_{{ strtolower(str_replace(' ', '_', $image->name)) }}" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel" aria-hidden="true">
                        <div class="modal-dialog modal-lg">
                            <div class="modal-content">
                                <div id="carousel-example-generic" class="carousel slide" data-ride="carousel">

                                    <!-- Wrapper for gallery slides -->
                                    <div class="carousel-inner" role="listbox">
                                        <?php $i = 1; ?>

                                        @if($image->privacy == 1)
                                            @foreach($image->images as $im)

                                                <div class="{{ ($i == 1) ? 'item active' : 'item'  }}">
                                                    <img src="{{  $im->path}}{{ $im['images']}}" class="img-responsive">
                                                </div>

                                                <?php $i++; ?>
                                            @endforeach
                                        @else
                                            <p>User has kept their gallery private</p>
                                        @endif
                                    </div>

                                    <!-- Controls -->
                                    <a class="left carousel-control" href="#carousel-example-generic" role="button" data-slide="prev">
                                        <span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span>
                                        <span class="sr-only">Previous</span>
                                    </a>
                                    <a class="right carousel-control" href="#carousel-example-generic" role="button" data-slide="next">
                                        <span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
                                        <span class="sr-only">Next</span>
                                    </a>
                                </div>

                            </div>
                        </div>
                    </div>
                @endforeach 

Gallery Images table

Gallery Table

Then I have relationships setup in my models, the first one is working, but the second one shows the modal but nothin inside it.

So I just wondered if any of you awesome people could help shed some light on this with me.

Thanks in advance...

0 likes
4 replies
simonhamp's avatar
Level 2

Because we discussed this on Slack, I wanted to post here to show the solution for others:

Your relationship between galleries and images wasn't enforced with any kind of referential integrity (i.e. no foreign key indexes in your DB) which meant your images weren't being related to the correct galleries.

Updating your DB records was the fix, but ultimately adding foreign keys using a DB engine that supports foreign key checks (e.g. InnoDB) would have helped to avoid this.

1 like
theUnforgiven's avatar

Yes it appeared that i had the wrong ID's after talking it through with @simonhamp he pointed this out, so kudos to him.

1 like
simonhamp's avatar

Happy to help... looks like that accidental @channel call didn't turn out too badly ;)

Please or to participate in this conversation.