How7o
  • Home
  • Tools
  • Prank Screens
  • Learn
  • Blog
  • Contact
Reading: Laravel Blade: Difference Between @foreach and @forelse
Share
How7oHow7o
Font ResizerAa
  • OS
Search
  • Home
  • Tools
  • Prank Screens
  • Learn
  • Blog
  • Contact
Follow US
© 2024–2026 How7o. All rights reserved.
How7o > Free Laravel, PHP, WordPress & Server Tutorials > Web Development > Laravel Blade: Difference Between @foreach and @forelse
Web Development

Laravel Blade: Difference Between @foreach and @forelse

how7o
By how7o
Last updated: May 23, 2026
4 Min Read
Laravel Blade @foreach vs @forelse comparison
SHARE

The difference between Blade’s @foreach and @forelse is what happens when the collection is empty. @foreach does nothing — the loop body simply doesn’t run. @forelse has an extra @empty branch that renders when the collection is empty, so you can show a “no items found” message in the same block.

Contents
  • Side by side
  • When to use which
  • Using $loop in both
  • @continue and @break
  • Frequently asked questions
  • Related guides
  • References

Last verified: 2026-05-17 on Laravel 11. Originally published 2024-03-28, rewritten and updated 2026-05-17.

Side by side

{{-- @foreach — empty collection renders nothing --}}
@foreach ($items as $item)
    <li>{{ $item->name }}</li>
@endforeach

{{-- @forelse — empty collection renders the @empty branch --}}
@forelse ($items as $item)
    <li>{{ $item->name }}</li>
@empty
    <li class="muted">No items found.</li>
@endforelse

@forelse saves you an extra @if ($items->isEmpty()) wrapper for the empty case. Use it whenever the empty-state message is part of the same UI block.

Blade @foreach vs @forelse — empty branch, $loop variable, @continue and @break

When to use which

  • @foreach when an empty collection means “render nothing for this section” — e.g. a sidebar widget that disappears entirely when there’s no data.
  • @forelse when an empty collection needs a visible “no results” message — search results, dashboards, lists where the user expects feedback that the query worked but returned nothing.

Using $loop in both

@forelse ($items as $item)
    <tr class="{{ $loop->odd ? 'bg-stripe' : '' }}">
        <td>{{ $loop->iteration }}</td>
        <td>{{ $item->name }}</td>
    </tr>

    @if (!$loop->last)
        <tr><td colspan="2"><hr></td></tr>
    @endif
@empty
    <tr><td colspan="2">No items.</td></tr>
@endforelse

The $loop variable is available inside both. Useful fields: iteration (1-based), index (0-based), first, last, odd, even, count, remaining.

@continue and @break

@forelse ($items as $item)
    @continue($item->archived)
    @break($loop->iteration > 100)

    <li>{{ $item->name }}</li>
@empty
    <li>No items.</li>
@endforelse

Both directives accept a condition argument — concise shorthand for @if (...) @continue @endif. Same syntax in plain @foreach.

Frequently asked questions

What does $loop contain inside @foreach/@forelse?

Both directives expose a special $loop variable inside the loop body with these fields: index (0-based), iteration (1-based), remaining, count, first, last, even, odd, depth (nesting depth), and parent (the outer loop’s $loop in nested cases). Useful for adding dividers between items: @unless($loop->last) <hr> @endunless.

Can I use @continue and @break in @forelse?

Yes — same as @foreach. @continue skips the rest of the current iteration; @break exits the loop. Both accept a condition: @continue($item->archived) is shorthand for @if ($item->archived) @continue @endif. @empty still fires if every iteration was continued and the collection was already empty.

Is @forelse only for arrays?

It works on anything iterable — arrays, Collections, query results, generators. The @empty branch fires when the iterable yields no items at all. For Eloquent collections specifically, $users->isEmpty() + @if/@else is an equivalent pattern; @forelse just combines the two.

Does @forelse compile to the same PHP as @foreach?

Roughly — Blade compiles @forelse ... @empty ... @endforelse to a check-then-foreach: $_empty = true; foreach ($items as $item) { $_empty = false; ... } if ($_empty) { ... }. The slight overhead is a single boolean — negligible. Use it freely wherever the readability is better.

Related guides

  • How Laravel’s old() Helper Works
  • How to Use Multiple where and orWhere in Laravel Eloquent
  • How to Get Multiple Rows with $wpdb in WordPress

References

Laravel Blade loops: laravel.com/docs/blade#loops. $loop variable: laravel.com/docs/blade#the-loop-variable.

TAGGED:BladeLaravelphp

Sign Up For Daily Newsletter

Be keep up! Get the latest breaking news delivered straight to your inbox.
[mc4wp_form]
By signing up, you agree to our Terms of Use and acknowledge the data practices in our Privacy Policy. You may unsubscribe at any time.
Share This Article
Facebook Copy Link Print
Previous Article Laravel database transactions explained Laravel Database Transactions Explained (DB::transaction vs beginTransaction)
Next Article Fix Ubuntu terminal not opening on VirtualBox Fix: Terminal Won’t Open in Ubuntu on VirtualBox
Leave a Comment

Leave a Reply Cancel reply

You must be logged in to post a comment.

FacebookLike
XFollow
PinterestPin
InstagramFollow
Most Popular
Run Laravel queue workers with Supervisor
How to Run Laravel Queue Workers in Production with Supervisor
May 23, 2026
Nginx as a reverse proxy for a Node.js app on Ubuntu
How to Set Up Nginx as a Reverse Proxy for Node.js on Ubuntu
May 23, 2026
Install and configure Redis on Ubuntu for Laravel and WordPress
How to Install and Configure Redis on Ubuntu (for Laravel & WordPress)
May 23, 2026
Harden a fresh Ubuntu VPS with UFW, Fail2Ban, and SSH key auth
How to Harden a Fresh Ubuntu VPS: UFW + Fail2Ban + SSH Key Auth
May 23, 2026
Set up Let's Encrypt SSL with Certbot on Ubuntu
How to Set Up Let’s Encrypt SSL with Certbot on Ubuntu (Apache & Nginx)
May 23, 2026

You Might Also Like

Laravel 403 forbidden on shared hosting — root htaccess rewrite into public folder
Web Development

Fix “403 Forbidden” on Laravel Shared Hosting

8 Min Read
WordPress logged-in menu swap — register_nav_menus + wp_nav_menu with is_user_logged_in ternary
Web Development

How to Display Different Menus to Logged-In Users in WordPress

7 Min Read
Laravel Eloquent exists method checking if a record exists in a database query
Web Development

How to Check if a Record Exists in Laravel

6 Min Read
Laravel user password being updated via artisan tinker with Hash::make
Web Development

How to Change a User Password in Laravel

6 Min Read
How7o

We provide tips, tricks, and advice for improving websites and doing better search.

Tools

  • Age Calculator
  • Word Counter
  • Image Upscaler
  • Password Generator
  • QR Code Generator
  • See all tools→

Pranks

  • Fake Blue Screen Prank
  • Hacker Typer
  • Fake iMessage Generator
  • Windows XP Crash Prank
  • Windows 11 Update Prank
  • See all prank screens →

Company

  • About Us
  • Blog
  • Contact
  • Privacy Policy
  • Terms of Service
  • Sitemap
© 2024–2026 How7o. All rights reserved.
Welcome Back!

Sign in to your account

Username or Email Address
Password

Lost your password?