How7o
  • Home
  • Tools
  • Prank Screens
  • Learn
  • Blog
  • Contact
Reading: How to Use LEFT JOIN in Laravel to Keep All Records
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 > How to Use LEFT JOIN in Laravel to Keep All Records
Web Development

How to Use LEFT JOIN in Laravel to Keep All Records

how7o
By how7o
Last updated: May 23, 2026
4 Min Read
Laravel leftJoin to keep all records even without matches
SHARE

To get all records from the main table in Laravel even when there’s no match in the joined table, swap join() for leftJoin(). The default join() is an inner join — it drops rows without a match. leftJoin() keeps every row from the left table and fills the right-side columns with NULL when there’s no match.

Contents
  • The fix
  • The four join types Laravel supports
  • Alternative — the four-arg join
  • Eloquent equivalent
  • Frequently asked questions
  • Related guides
  • References

Last verified: 2026-05-17 on Laravel 11. Originally published 2023-01-31, rewritten and updated 2026-05-17.

The fix

$funds = DB::table('funds')
    ->select(
        'funds.id',
        'funds.package_code',
        'funds.amount',
        'funds.method',
        'funding_sources.name',
        'funding_sources.estimated_cost',
        'funding_sources.contract_value',
        'funding_sources.party_name'
    )
    ->leftJoin('funding_sources', 'funding_sources.id', '=', 'funds.funding_source_id')
    ->get();

Every row in funds comes back. Funds without a corresponding funding_sources record get NULL for name, estimated_cost, contract_value, and party_name.

Laravel join types — join (inner), leftJoin (left outer), rightJoin, with Eloquent relationships

The four join types Laravel supports

  • join() — inner join. Default. Drops unmatched rows.
  • leftJoin() — left outer join. Keeps every row from the main table; NULLs on the right when no match.
  • rightJoin() — right outer join. Keeps every row from the joined table; NULLs on the left when no match. Rarely used; usually it’s clearer to swap which table is the main one and use a leftJoin.
  • crossJoin() — Cartesian product. Every row from each table paired with every row from the other. Useful for generating combinations; almost never what you want by accident.

Alternative — the four-arg join

// Same result as leftJoin
->join('funding_sources', 'funding_sources.id', '=', 'funds.funding_source_id', 'left')

The four-argument join takes the join type as the fifth element. Both 'left' and 'left outer' work and produce identical SQL. leftJoin() is the dedicated shortcut.

Eloquent equivalent

// app/Models/Fund.php
public function fundingSource()
{
    return $this->belongsTo(FundingSource::class);
}

// Controller
$funds = Fund::with('fundingSource')->get();

// In Blade / view
@foreach ($funds as $fund)
    {{ $fund->package_code }} —
    {{ $fund->fundingSource?->name ?? 'No source assigned' }}
@endforeach

For relationship navigation, Eloquent’s with() eager-loads the related record. Funds without a source get $fund->fundingSource as null — the nullsafe operator ?-> handles it cleanly. Use this when you don’t need the joined columns flattened into the same row.

Frequently asked questions

What does join() default to in Laravel?

Inner join. join('table', 'a.id', '=', 'b.id') only returns rows where the join condition matches on both sides. Use leftJoin (or rightJoin) when you want unmatched rows from one side to still appear, with the other side as NULL.

What’s the difference between leftJoin and join(..., 'left outer')?

None — they produce the same SQL. leftJoin is a thin wrapper around join() with the fourth argument set to 'left'. Use leftJoin for readability; the four-arg form is mostly there for code that builds queries dynamically and needs to switch join types based on a variable.

How do I detect NULL columns from the missing side?

After a leftJoin, rows without a match have NULL in every column from the right-hand table. In Blade or PHP: $fund->name ?? 'No funding source'. In a query filter: ->whereNotNull('funding_sources.id') to keep only matched rows, or ->whereNull('funding_sources.id') to find unmatched ones (the “anti-join” pattern).

Should I use Eloquent relationships or raw leftJoin?

Eloquent relationships (hasOne, belongsTo) generate cleaner code for the common case — Fund::with('fundingSource')->get() handles the missing-match case naturally (the relationship is just null). Reach for leftJoin when you need the joined columns inside the same row (for filtering, sorting, or selecting specific fields) instead of as a nested relation.

Related guides

  • How to Do a Left Outer Join with Laravel
  • How to Use Multiple where and orWhere in Laravel Eloquent
  • How to Use orderBy in Laravel Eloquent

References

Laravel query builder joins: laravel.com/docs/queries#joins. Eloquent relationships: laravel.com/docs/eloquent-relationships.

TAGGED:Laravelmysqlphp

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 Securely upload only image files in PHP How to Upload Only Image Files Using PHP
Next Article Make Select2 work inside a Bootstrap modal How to Use Select2 Inside a Bootstrap Modal
Leave a Comment

Leave a Reply Cancel reply

You must be logged in to post a comment.

FacebookLike
XFollow
PinterestPin
InstagramFollow
Most Popular
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
Tailscale mesh — peer-to-peer connections between devices, coordination server
How to Install Tailscale on Ubuntu (Zero-Config Mesh VPN for Self-Hosters)
May 24, 2026

You Might Also Like

Check if an element is hidden or visible with jQuery
Web Development

How to Check if an Element Is Hidden or Visible with jQuery

4 Min Read
Open a file selection dialog from a button click
Web Development

How to Open a File Dialog When Clicking a Button

4 Min Read
aaPanel MySQL root password reset via the Databases page
Server Management

How to Reset the MySQL Root Password in aaPanel

6 Min Read
Check if Laravel scheduler is running (cron + php artisan schedule:run)
Web Development

How to Check if Laravel Scheduler Is Running (Cron + Logs)

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?