How7oHow7o
  • Home
  • Tools
    • All Tools
    • Image Tools
    • Text & String Tools
    • Video Tools
    • Developer Tools
    • PDF Tools
    • Calculators & More
  • Prank Screens
  • Learn
  • Blog
  • Contact
Reading: Laravel Blade: Difference Between @foreach and @forelse
Share
Notification Show More
Font ResizerAa
How7oHow7o
Font ResizerAa
  • Tools
  • Prank Screens
  • Learn
  • Blog
Search
  • Home
  • Tools
    • All Tools
    • Image Tools
    • Text & String Tools
    • Video Tools
    • Developer Tools
    • PDF Tools
    • Calculators & More
  • 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.

You Might Also Like

WordPress search users by multiple fields — WP_User_Query search_columns + meta_query
Web Development

How to Search Users by Multiple Fields in WordPress

7 Min Read
Laravel Eloquent count rows — Post::count query snippet with aggregate bar chart icon
Web Development

How to Count Rows in Laravel Eloquent Efficiently

6 Min Read
WooCommerce dynamic currency switcher — cookie-stored currency applied via woocommerce_currency filter
Web Development

How to Dynamically Change Currency in WooCommerce

7 Min Read
Nginx 502 with recv() failed 104 connection reset — PHP-FPM timeout and memory tuning
Server Management

Fix Nginx ‘recv() failed (104: Connection reset by peer)’ with FastCGI

9 Min Read
How7oHow7o

Free browser-based tools, prank screens, and step-by-step tech tutorials. No signup, nothing uploaded.

  • Private by design — your files never leave your device
  • Instant — no signup, no install, no waiting
  • Always free — no trials, no watermarks, no paywalls

Tools

  • PDF Editor
  • Image Upscaler
  • JPEG Compressor
  • Password Generator
  • QR Code Generator
  • Video Trimmer
  • See all tools →

Pranks

  • Cracked Screen Prank
  • Fake Blue Screen (BSOD)
  • Fake Disk Format
  • Fake Low Battery
  • Fake Nintendo Switch Update Screen
  • Fake PlayStation Update Screen
  • 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?