How to use Blade's control structures like @foreach, @for, @while, and @switch in Laravel?

In Laravel, Blade's control structures like @foreach, @for, @while, and @switch provide a convenient way to loop through data, iterate over a set number of times, execute code while a condition is true, or switch between different cases. Here's how you can use these control structures:

  1. @foreach: The @foreach loop allows you to iterate over arrays or collections.
@foreach($array as $item) // code to be executed for each $item @endforeach

You can also access the index using $loop variable:

@foreach($array as $item) {{ $loop->index }}: {{ $item }} @endforeach
  1. @for: The @for loop allows you to iterate a specific number of times.
@for($i = 0; $i < 5; $i++) // code to be executed for each iteration @endfor
  1. @while: The @while loop allows you to execute code while a condition is true.
@while($condition) // code to be executed while $condition is true @endwhile
  1. @switch: The @switch control structure is used to perform different actions based on different cases.
@switch($variable) @case('value1') // code to be executed if $variable equals 'value1' @break @case('value2') // code to be executed if $variable equals 'value2' @break @default // code to be executed if $variable doesn't match any cases @endswitch

These control structures can be used within your Laravel Blade templates to handle complex logic, iterate through data, and execute code based on certain conditions. Remember to use the correct syntax and properly close the control structure using the respective ending directive.