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
Laravel Eloquent ORM — a model class mapping to a database table with query methods
Laravel Eloquent ORM: The Complete Guide to Querying Your Database
June 16, 2026
Set vi as the default editor in Ubuntu — a terminal opening the vim editor
How to Set vi (Vim) as the Default Editor in Ubuntu
June 8, 2026
rsync says ALL DONE but files are missing — a terminal showing ALL DONE next to an empty folder
rsync Says “ALL DONE” but Files Are Missing: How to Verify
June 8, 2026
Migrate a website to a new server with rsync — files copying from an old server to a new one over SSH
How to Migrate a Website to a New Server With rsync
June 8, 2026
Bun runtime — faster JS toolkit replacing npm in Laravel projects
How to Install Bun Runtime on Ubuntu (And Use It in a Laravel Project)
May 24, 2026

You Might Also Like

WordPress wpdb get_results returns multiple rows
Web Development

How to Get Multiple Rows with $wpdb in WordPress

5 Min Read
Fix MySQL CONCAT returning NULL with COALESCE or CONCAT_WS
Web Development

How to Handle MySQL CONCAT Returning NULL

4 Min Read
Replace strings in a MySQL database with UPDATE and REPLACE()
Web Development

How to Replace Strings in a MySQL Database

5 Min Read
Trigger a function when DataTables loads data via Ajax
Web Development

How to Trigger a Function When DataTables Loads Data

5 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?