How Can I Use Laravel Blade Foreach’s Break and Continue?

Posted by

This lesson will give you an example of how to use the Laravel Blade Continue. Describe the Laravel Blade foreach break step-by-step. I will demonstrate how to use the continue and break in latches on a laravel blade. The use of continue and break in the Laravel foreach loop is demonstrated. To continue the Laravel cycle, you will perform the following actions.

The @break and @continue directives in @foreach loops in Laravel Blade templates allow you to regulate the loop’s flow in the following ways:

@break: This directive is used to break out of the current @foreach loop early, so that the code that follows the loop can begin.

@continue: With this directive, the current iteration of the @foreach loop is skipped, and the next iteration is started without running the current iteration’s code.

You now have additional control over how you handle and present data from your arrays or collections by using these directives to incorporate conditional logic into your Blade @foreach loops.

The basic blade file code and output are visible here:

Blade File Code:

<!DOCTYPE html>
<html>
<head>
    <title>How to Use Break And Continue In Laravel Blade Foreach? - </title>
    <link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/5.0.1/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
       
<div class="container">
    <h1>How to Use Break And Continue In Laravel Blade Foreach?</h1>
  
    <ul>
    @foreach ($users as $user)
        @if ($user->type == 1)
            @continue
        @endif
  
        <li>{{ $user->id }}. {{ $user->name }}</li>
  
        @if ($user->id == 8)
            @break
        @endif
    @endforeach
    </ul>
</div>
       
</body>
      
</html>

Output:

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x