0

I want to use Bootstrap carousel and the images should come from my database

<div id="carouselExampleControls" class="carousel slide" data-ride="carousel">
    <div class="carousel-inner">
        @foreach($property->photos as $photo)
            <div class="carousel-item">
                <img class="d-block w-100" src="{{$photo->path}}" alt="First slide">
            </div>
        @endforeach 
    </div>
</div>

When I inspect the page I can see that the image is there, however I will not see anything of it because I need the class active in the div with class carousel-item.

Is there a way I can add the class active on the first box?

I tried using jQuery but it does not work:

$('.carousel-inner .carousel-item:first-child').addClass('active');
2
  • What version of Laravel are you using? Commented Aug 19, 2018 at 11:48
  • Am using version 5.6 Commented Aug 19, 2018 at 11:51

2 Answers 2

4

You can use The loop variable to accomplish this:

@foreach($property->photos as $photo)
    <div class="carousel-item {{ $loop->first ? 'active' : '' }}">
        <img class="d-block w-100" src="{{$photo->path}}" alt="First slide">
    </div>
@endforeach 
Sign up to request clarification or add additional context in comments.

1 Comment

That helps too. Appreciate the support
2

Do you use

$(document).ready(function(){

like

$(document).ready(function(){
    $('.carousel-inner .carousel-item:first-child').addClass('active');
}

or not? because for run jq code you need wait for document to ready!

1 Comment

I use yes, but now that you made double check I just noticed that I was placing the code inside a wrong function. Appreciate the support

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.