Laravel 中的动态滑块

Dynamic Sliders In Laravel

我正在尝试使用 Laravel 制作基本幻灯片。

必须在第一张幻灯片的 class 部分 active。我正在尝试这段代码,但它没有用。因为它会为所有幻灯片的 class 添加活动。这个问题的解决方案是什么?

@foreach($slides as $slayt)
<div class="carousel-item active">
            <img src="{{ $slayt->image }}" alt="slide">
    <div class="container">
      <div class="carousel-caption">
        <h3>{{ $slayt->title }}</h3>
        <p>{{ $slayt->body }}</p>
      </div>
    </div>
  </div>
@endforeach

我原来的HTML代码:

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

    <div class="carousel-inner">
      
      <div class="carousel-item active">
                <img src="http://localhost:8000/storage/slides/January2021/YFgRC9i7iepG0S5aCph3.jpg" alt="slide">
        <div class="container">
          <div class="carousel-caption">
            <h3>Birinci Slayt</h3>
            <p>Cras justo odio, dapibus ac facilisis in, egestas eget quam. Donec id elit non mi porta gravida at eget metus. Nullam id dolor id nibh ultricies vehicula ut id elit.</p>
          </div>
        </div>
      </div>

      <div class="carousel-item">
                <img src="http://localhost:8000/storage/slides/January2021/YFgRC9i7iepG0S5aCph3.jpg" alt="slide">
        <div class="container">
          <div class="carousel-caption">
            <h3>İkinci Slayt</h3>
            <p>Cras justo odio, dapibus ac facilisis in, egestas eget quam. Donec id elit non mi porta gravida at eget metus. Nullam id dolor id nibh ultricies vehicula ut id elit.</p>
          </div>
        </div>
      </div>

      <div class="carousel-item">
                <img src="http://localhost:8000/storage/slides/January2021/YFgRC9i7iepG0S5aCph3.jpg" alt="slide">
        <div class="container">
          <div class="carousel-caption">
            <h3>Üçüncü Slayt</h3>
            <p>Cras justo odio, dapibus ac facilisis in, egestas eget quam. Donec id elit non mi porta gravida at eget metus. Nullam id dolor id nibh ultricies vehicula ut id elit.</p>
          </div>
        </div>
      </div>

    </div>


    <a class="carousel-control-prev" 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" 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>

像这样使用 $loop->first (official docs):

@foreach($slides as $slayt)
@if ($loop->first)
    <div class="carousel-item active">
        <img src="{{ $slayt->image }}" alt="slide">
        <div class="container">
            <div class="carousel-caption">
                <h3>{{ $slayt->title }}</h3>
                <p>{{ $slayt->body }}</p>
            </div>
        </div>
    </div>
@else
    <div class="carousel-item">
        <img src="{{ $slayt->image }}" alt="slide">
        <div class="container">
            <div class="carousel-caption">
                <h3>{{ $slayt->title }}</h3>
                <p>{{ $slayt->body }}</p>
            </div>
        </div>
    </div>
@endif
@endforeach

为了不复制整个 html,您还可以在 if 检查

中包含 active class
@foreach($slides as $slayt)
    <div class="carousel-item @if($loop->first) active @endif">
        <img src="{{ $slayt->image }}" alt="slide">
        <div class="container">
            <div class="carousel-caption">
                <h3>{{ $slayt->title }}</h3>
                <p>{{ $slayt->body }}</p>
            </div>
        </div>
    </div>
@endforeach