Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

declaring simple variables in views in Laravel

Tags:

This is probably a simple thing, but I don't know how to declare and increment an integer variable in a view in Laravel.

I have a couple foreach loops that I am using:

@foreach($fans as $fan)     @foreach ($array as $x)          @if($fan->fbid==$x)           @endif     @endforeach   @endforeach 

I would like to throw in an integer variable $a, that counts the number of times it makes it through the if statement. Like:

$a=0; @foreach($fans as $fan)         @foreach ($array as $x)              @if($fan->fbid==$x)              $a++;                                @endif         @endforeach   @endforeach  {{$a}} 

What is the proper syntax for doing this in a view in Laravel? Thank you.

like image 449
user1072337 Avatar asked Jul 07 '13 06:07

user1072337


People also ask

How do you declare a variable in Laravel blade template?

To define a variable in the Laravel Blade template you can make use of the @php directive. This directive can be used to define one to many variables and you can define it like the following.

What is @yield used for in Laravel?

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.


1 Answers

The Blade {{ }} will echo out what you are doing.

You should do it like this:

<?php $a = 0; ?> @foreach($fans as $fan)         @foreach ($array as $x)              @if ($fan->fbid == $x)                  <?php $a++; ?>                                @endif         @endforeach @endforeach  {{$a}} 
like image 143
Patrick Reck Avatar answered Oct 06 '22 00:10

Patrick Reck