Ad

Dynamic Carousel In Laravel Not Working Displays Only One Image

- 1 answer

I'm trying to create a dynamic carousel in laravel but i see only one image. I have looking for solutions from many hours but still i'm stucked

I have tried to change my code from this solution Dynamic carousel in PHP Laravel only shows one image but didn't work

home.blade.php code

<div id="myCarousel" class="carousel slide" data-ride="carousel">
<ol class="carousel-indicators">
<li data-target="#myCarousel" data-slide-to="0"     class="active">                                                                   </li>
</ol>

<div class="carousel-inner" role="listbox">
@foreach($sliders as $slider)
<div class="carousel-item active">
<img src="{{url('images', $slider->image)}}" class="d-block w-100"  alt="..."> 
</div>
@endforeach

<a class="carousel-control-prev" target="_blank" rel="nofollow noreferrer" href="#myCarousel" role="button"  data-slide="prev">
<span class="carousel-control-prev-icon" aria-hidden="true">     </span>
    <span class="sr-only">Previous</span>
</a>
<a class="carousel-control-next" target="_blank" rel="nofollow noreferrer" href="#myCarousel" role="button" data-slide="next">
<span class="carousel-control-next-icon" aria-hidden="true"></span>
<span class="sr-only">Next</span>
</a>
</div>

master.blade.php code

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-  scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="icon" target="_blank" rel="nofollow noreferrer" href="{{asset('favicon.ico')}}">
<title>1233</title>


<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js"   integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script  src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
<link rel="stylesheet"   target="_blank" rel="nofollow noreferrer" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.  css" integrity="sha384-  Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm"   crossorigin="anonymous">
<script     src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.  min.js" integrity="sha384-   ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q"        crossorigin="anonymous"></script>

</head>
<body>

@include('front.nav')
@yield('content')
@include('front.footer')
</body>
</html>

Any help will be appreciated.

Ad

Answer

The issue why its not working is because the loop

@foreach($sliders as $slider)
  <div class="carousel-item active">
     <img src="{{url('images', $slider->image)}}" class="d-block w-100"  alt="..."> 
  </div>
@endforeach

is adding active class to all the images. You only need active on a single image you can set active on a single using some php workaround like this.

<div id="myCarousel" class="carousel slide" data-ride="carousel">
    <ol class="carousel-indicators">
        <li data-target="#myCarousel" data-slide-to="0" class="active"></li>
    </ol>
    <div class="carousel-inner">
        @foreach($sliders as $key => $slider)
        <div class="carousel-item {{$key == 0 ? 'active' : '' }}">
            <img src="{{url('images', $slider->image)}}" class="d-block w-100"  alt="..."> 
        </div>
        @endforeach
    </div>
    <a class="carousel-control-prev" target="_blank" rel="nofollow noreferrer" href="#myCarousel" role="button"  data-slide="prev">
        <span class="carousel-control-prev-icon" aria-hidden="true">     </span>
        <span class="sr-only">Previous</span>
    </a>
    <a class="carousel-control-next" target="_blank" rel="nofollow noreferrer" href="#myCarousel" role="button" data-slide="next">
        <span class="carousel-control-next-icon" aria-hidden="true"></span>
        <span class="sr-only">Next</span>
    </a>
</div>

also you forgot to close the <div class="carousel-inner"></div> tag.

Ad
source: stackoverflow.com
Ad