Dynamic carousel in PHP Laravel only shows one ima

2019-08-30 02:16发布

问题:

I'm trying to create a dynamic carousel in laravel but I see only one image. I have looked for solutions for hours but I'm stuck

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

This is my blade code

@extends('front.master')

@section('content')

<main role="main">

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

<section class="jumbotron text-center">
</section>

<div class="album py-5 bg-light">
<div class="container">


</div>

</main>

@endsection

Any help will be appreciated.

回答1:

Your closing tag of your <div class="carousel-item"> is inside the loop, while the starting tag is not. You need to loop with <div class="carousel-item"> instead of just image.

change this

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

to this

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