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.
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.
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.
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}}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With