How7oHow7o
  • Home
  • Tools
    • All Tools
    • Image Tools
    • Text & String Tools
    • Video Tools
    • Developer Tools
    • PDF Tools
    • Calculators & More
  • Prank Screens
  • Learn
  • Blog
  • Contact
Reading: How to Use LEFT JOIN in Laravel to Keep All Records
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 > 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.

You Might Also Like

Store a PHP array in a MySQL database with JSON
Web Development

How to Store a PHP Array in a MySQL Database

5 Min Read
React.createElement conditional rendering with && short-circuit
Web Development

Conditional Rendering with React.createElement

6 Min Read
Rename menu items on the WooCommerce My Account page
Web Development

How to Rename Menu Items on the WooCommerce My Account Page

4 Min Read
MySQL 8 create user and grant privileges on Ubuntu
Web Development

How to Create Users and Grant Privileges in MySQL 8 on Ubuntu

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