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

Extract a .tar.gz archive in PHP with PharData
Web Development

How to Extract a .tar.gz Archive in PHP

5 Min Read
Laravel DataTables custom column search — filterColumn callback handles the search SQL
Web Development

How to Search Custom or Composite Columns in Laravel DataTables

8 Min Read
Prevent tab key from focusing on a link with tabindex=-1
Web Development

How to Skip a Link When Pressing Tab (tabindex=-1)

5 Min Read
Break out of a jQuery .each() loop with return false
Web Development

How to Break Out of a jQuery .each() Loop

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?