Laravel Blade: Increment variable by 1 each time?
Asked Answered
P

7

26

Using Laravel blade template, is there a way to include a variable and increase each time in the foreach or what is better approach?

For example:

@foreach($categories as $category)
  <li><a href="#tab_c1" role="tab" data-toggle="tab">{{$category->name}}</a></li>
@endforeach

In the foreach block, the value from #tab_c1 will need to be increase. eg: #tab_c1, #tab_c2, #tab_c3

Psychopath answered 18/6, 2015 at 15:12 Comment(0)
B
36

Add iterator to @foreach:

@foreach($categories as $key => $category)
  <li @if ($key === 0) class="active" @endif>
    <a href="#tab_c{{$key+1}}" role="tab" data-toggle="tab">
      {{$category->name}}
    </a>
  </li>
@endforeach

{{$key+1}} in my example because in PHP iterator starts at 0.

Barrelchested answered 18/6, 2015 at 15:15 Comment(1)
Thats great, thanks. Is there a way to include class="active" on the first <li> only?Fat
S
30

Since Laravel 5.3 you can use The Loop Variable, $loop->iteration for concrete situation. https://laravel.com/docs/5.3/blade#the-loop-variable

Example:

@foreach ($questions as $question)
    <tr>
        <th scope="row">{{ $loop->iteration }}</th>
        <td>{{ $question->question }}</td>
        <td>{{ $question->category_id }}</td>
    </tr>
@endforeach
Sensitize answered 5/11, 2016 at 20:3 Comment(0)
L
12

You can try this:

@php($count=0)

@foreach($unit->materials as $m)
    @if($m->type == "videos")
        @php($count++)
    @endif
@endforeach

{{$count}}
Lucania answered 22/10, 2019 at 6:18 Comment(0)
I
10

Add a key value in the foreach loop

@foreach($questions as $key => $question)
<tr>
    <th scope="row">{{ ++$key }}</th>
    <td>{{ $question->question }}</td>
    <td>{{ $question->category_id }}</td>
</tr>
@endforeach
Infatuate answered 2/3, 2017 at 6:17 Comment(0)
D
10

Just use {{ $loop->iteration }} to iterate from 1 to limit

@foreach($categories as $category)
  <li><a href="#tab_c{{ $loop->iteration }}" role="tab" data-toggle="tab">{{$category->name}}</a></li>
@endforeach
Decennium answered 8/8, 2017 at 8:12 Comment(0)
L
5

Just use the key value. For most arrays that will just be 0 up.

@foreach($categories as $i => $category)
  <li{{ $i == 0 ? ' class="active"' : '' }}><a href="#tab_c{{ $i }}" role="tab" data-toggle="tab">{{$category->name}}</a></li>
@endforeach
Lookout answered 18/6, 2015 at 15:13 Comment(2)
Thats great, thanks. Is there a way to include class="active" on the first <li> only?Fat
Sure, just use a ternary operator. I've updated my answer.Lookout
N
1

This should do the trick @php is same as php open and close tags in laravel

<?php $count=0; ?>
 @php($count=0)

<table>
<th>#</th>
<th>Category Name</th>
<tbody>
@php($count=0)
@foreach($categories as $category)
@php($count++)
<tr>
<td>{{$count}}</td>
<td>{{$category->name}}</td>
</tr>
@endforeach
</tbody>
</table>
Ninette answered 21/8, 2020 at 10:36 Comment(1)
Please add an explanation for your solution (e.g. explain what the trick is, what the @php function does, etc...)Diffract

© 2022 - 2024 — McMap. All rights reserved.