Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel Blade: Increment variable by 1 each time?

Tags:

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

like image 932
I'll-Be-Back Avatar asked Jun 18 '15 15:06

I'll-Be-Back


People also ask

What is yield in Laravel blade?

In Laravel, @yield is principally used to define a section in a layout and is constantly used to get content from a child page unto a master page.

How do you increment for each loop?

Remember that you'll have to define $i = 0 before the foreach loop if you want to start counting/incrementing from 0. Voting is disabled while the site is in read-only mode. Voting is disabled while the site is in read-only mode. Show activity on this post.

What is Laravel blade syntax?

The Blade is a powerful templating engine in a Laravel framework. The blade allows to use the templating engine easily, and it makes the syntax writing very simple. The blade templating engine provides its own structure such as conditional statements and loops.


1 Answers

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.

like image 196
Limon Monte Avatar answered Sep 22 '22 07:09

Limon Monte