简体   繁体   English

laravel 循环将活动类添加到循环的第一项

[英]laravel loop add active class to first item of loop

I'm trying to display dynamic data in bootstrap tabs.我正在尝试在引导程序选项卡中显示动态数据。 For that I want to add active class to the first item.为此,我想将活动类添加到第一项。 How can I do that??我怎样才能做到这一点?? My blade code我的刀片代码

 <div id="rootwizard">
                                    <ul class="nav nav-tabs wizard" role="tablist">
                                    @foreach( $places as $place)
                                            <li class=" active" style=""><a href="#{{$place->id}}" role="tab" data-toggle="tab">{{$place->name}}</a></li>
                                    @endforeach
                                         
                                    </ul>


                                    <div class="tab-content">
                                            @foreach($places as $place)
                                            <div class="tab-pane fade active in" id="{{$place->id}}">
                                                    <div class="row">
                                                        <div class="col col-md-4">
                                                            <img class="img-responsive" src=" 
                                 {{'/storage/places/'.$place->image}}" alt="">
                                                        </div>
                                                        <div class="col col-md-4">
                                                  
                                                                {{$place->intro}}
                                                          
                                              <a href="#" class="btn btn-info">Read More</a>
                                                        </div>
                                                        <div class="col col-md-4">
         <img class="img-responsive" src="{{'front/images/maps/map-sample.PNG'}}" alt="map">
                                                        </div>                  
                                                    </div>
                                            </div>
                                            @endforeach

And my controller还有我的控制器

 $place = Place::orderby('id', 'asc')->get();

Modify your @foreach() loop to include an index, then add the active class based on that:修改您的@foreach()循环以包含索引,然后基于该索引添加active类:

@foreach($places as $index => $place)
  <li class="{{ $index == 0 ? 'active' : '' }}" style="">
    <a href="#{{ $place->id }}" role="tab" data-toggle="tab">{{ $place->name }}</a>
  </li>
  ...
@endforeach

...

@foreach($places as $index => $place)
  <div class="tab-pane fade {{ $index == 0 ? 'active in' : '' }}" id="{{ $place->id }}">
  ...
@endforeach

As per @ThomasVanderVeen's suggestion, you can use the built-in $loop variable to accomplish this without the need for the extra $index variable:根据@ThomasVanderVeen 的建议,您可以使用内置的$loop变量来完成此操作,而无需额外的$index变量:

@foreach($places as $place)
  <li class="{{ $loop->first ? 'active' : '' }}" style="">
    <a href="#{{ $place->id }}" role="tab" data-toggle="tab">{{ $place->name }}</a>
  </li>
  ...
@endforeach

...

@foreach($places as $place)
  <div class="tab-pane fade {{ $loop->first ? 'active in' : '' }}" id="{{ $place->id }}">
  ...
@endforeach

Documentation for the $loop variable can be found here: $loop变量的文档可以在这里找到:

https://laravel.com/docs/8.x/blade#the-loop-variable https://laravel.com/docs/8.x/blade#the-loop-variable

Start the @foreach loop after <ul tag will solve your problem.在 <ul 标签之后启动 @foreach 循环将解决您的问题。 Otherwise, it will active all the <a elements.否则,它将激活所有 <a 元素。

            <div class="col-lg-3">

                <ul class="nav nav-tabs flex-column">
                    @foreach($facilities as $index => $team)
                    <li class="nav-item {{ ($index == 0) ? 'active show':'' }}">
                        <a class="nav-link" data-toggle="tab" href="#tab-{{ $team->id }}">{{$team->title}}</a>
                    </li>
                    @endforeach
                </ul>

            </div>
            <div class="col-lg-9 mt-4 mt-lg-0">
                <div class="tab-content">
                    @foreach($facilities as $index => $team)
                    <div class="tab-pane {{ ($index == 0) ? 'active show':'' }}" id="tab-{{ $team->id }}">
                        <div class="row">
                            <div class="col-lg-8 details order-2 order-lg-1">
                                <h3>{{$team->title}}</h3>

                                <p>{{$team->message}}</p>
                            </div>
                            <div class="col-lg-4 text-center order-1 order-lg-2">
                                <img src="{{asset('storage/service/'.$team->image)}}" alt="" class="img-fluid">
                            </div>
                        </div>
                    </div>
                    @endforeach
                </div>

            </div>

    </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM