laravel-10-featured

Conditional rendering & loop in Blade template – Turotiral 5

There are special directives in the Balde template for each PHP conditional logics like if.. else, issetetc.

You can read more in Laravel’s official documentation here.

Conditional rendering directives are:

  • @if.. @elseif..@else.. @endif
  • @unless()…@endunless – Condition has to be false
  • @isset()…@endisset
  • @php ….@endphp – To write some PHP code into view file

Loop directives are:

  • @for…@endfor
  • @while…@endwhile
  • @foreach…@endforeach
  • @switch…@case…@default…@endswitch
  • @forelse…@empty…@endforelse
  • @while…@endwhile
  • @continue
  • @break

Also $loop variable has variety of useful properties like:

$loop->indexThe index of the current loop iteration (starts at 0).
$loop->iterationThe current loop iteration (starts at 1).
$loop->remainingThe iterations remaining in the loop.
$loop->countThe total number of items in the array being iterated.
$loop->firstWhether this is the first iteration through the loop.
$loop->lastWhether this is the last iteration through the loop.
$loop->evenWhether this is an even iteration through the loop.
$loop->oddWhether this is an odd iteration through the loop.
$loop->depthThe nesting level of the current loop.
$loop->parentWhen in a nested loop, the parent’s loop variable.