How to Set Variables in a Laravel Blade Template?

Posted by LaravelIndia - 4 years ago

Declaring simple variables in views in Laravel

Example :1

<?php $a = 0; ?>
@foreach($products as $prd)
        @foreach ($array as $x)
             @if ($prd->pbid == $x)
                 <?php $a++; ?>                  
             @endif
        @endforeach
@endforeach

{{$a}}

Example :2

@php
     enter your codes here
@endphp

assign a value to a variable in blade file

@if($manager=='2')
  @php $status='1'; @endphp
@endif

@if($manager=='3')
  @php $status='2'; @endphp
@endif

@if($manager=='4')
  @php $status='3'; @endphp
@endif