Skip to content
Advertisement

Blade Foreach alternating classes

I am using a blade template and I was wanting to know within my foreach what the best way is for me to have it so that my first .panel-heading has another class added to it and then the second one has another etc

Example:

.red .blue .red .blue .red

Code:

@foreach ($dealsDB as $deal)
    <div class="col-md-4">
        <div class="panel panel-default">
            <div class="panel-heading">
                <h4><i class="fa fa-fw fa-gift"></i>{{ $deal->title }}</h4>
            </div>
            <div class="panel-body">
                <p>{{ $deal->content }}</p>
            </div>
        </div>
    </div>
@endforeach 

Advertisement

Answer

try this

$k=0;
@foreach ($dealsDB as $deal)
        <div class="col-md-4">
            <div class="panel panel-default">
                <div class="panel-heading @if($k%2==0) red @else blue @endif">
                    <h4><i class="fa fa-fw fa-gift"></i>{{ $deal->title }}</h4>
                </div>
                <div class="panel-body">
                    <p>{{  $deal->content }}</p>
                </div>
            </div>
        </div>
   $k++;
@endforeach 
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement