How7o
  • Home
  • Tools
  • Prank Screens
  • Contact
  • Blog
Reading: How to Check if a Record Exists in Laravel
Share
Subscribe Now
How7oHow7o
Font ResizerAa
  • Marketing
  • OS
  • Features
  • Guide
  • Complaint
  • Advertise
Search
  • Home
  • Tools
  • Prank Screens
  • Contact
  • Blog
Follow US
Copyright © 2014-2023 Ruby Theme Ltd. All Rights Reserved.
How7o > Blog > Web Development > How to Check if a Record Exists in Laravel
Web Development

How to Check if a Record Exists in Laravel

how7o
By how7o
Last updated: April 19, 2026
6 Min Read
Laravel Eloquent exists method checking if a record exists in a database query
SHARE

Checking whether a record exists in Laravel seems like a one-liner, but the method you pick matters: exists(), count(), and first() behave differently under the hood, and picking the wrong one means loading an entire row into memory when you only needed a boolean. In this guide, I’ll show you four idiomatic ways to check for a record’s existence in Laravel, and when each one is the right choice.

Contents
  • TL;DR
  • Option 1: exists() — The Right Default
  • Option 2: count() — When You Need the Actual Number
  • Option 3: first() — When You Need the Row Anyway
  • Option 4: firstOrFail() — When Absence Is an Error
  • Which One Should You Use?
  • Troubleshooting
    • Call to Undefined Method exists()
    • Race Condition Between Exists Check and Insert
  • Frequently Asked Questions
  • Related Guides

Originally published January 31, 2023, rewritten and updated April 17, 2026.

TL;DR

Use Model::where(...)->exists() when you only need a boolean — it runs a lightweight SELECT EXISTS(...) query that returns without pulling any columns. Use first() when you actually need the row. Only reach for count() when the count itself matters.

Option 1: exists() — The Right Default

Eloquent’s exists() method is the direct answer. It runs a SQL SELECT EXISTS(...) query that returns a boolean without fetching any columns — so it stays cheap even on very wide rows.

if (User::where('email', '=', $email)->exists()) {
    // user found
}

There’s also a convenience inverse, doesntExist(), which reads more naturally when you want the negative branch:

if (User::where('email', '=', $email)->doesntExist()) {
    // handle the "no user" case
}

Option 2: count() — When You Need the Actual Number

If you need the count itself (e.g. to show “3 results”), use count(). Don’t use it purely as an existence check — count() scans all matching rows, while exists() stops at the first hit.

$count = User::where('email', '=', $email)->count();

if ($count > 0) {
    // user found
}

On a unique column like email, the performance difference is negligible. On a non-unique column with many matches, the gap widens — exists() is faster because the database can short-circuit the query after finding the first match.

Option 3: first() — When You Need the Row Anyway

If the next thing you’re going to do is use the record, skip the separate existence check and go straight to first(). It returns either the model or null:

$user = User::where('email', '=', $email)->first();

if ($user !== null) {
    // use $user directly
}

Running exists() followed by first() is wasteful — that’s two round-trips to the database for information one query could have given you.

Comparison of Laravel exists, count, and first methods for checking if a record exists

Option 4: firstOrFail() — When Absence Is an Error

In controller actions where a missing record should trigger a 404, use firstOrFail(). It throws Illuminate\Database\Eloquent\ModelNotFoundException if no row matches, which Laravel’s exception handler converts to a 404 response automatically:

$user = User::where('email', '=', $email)->firstOrFail();
// if we reach here, $user is guaranteed to be a model instance

This lets you skip the null check entirely and removes the “phantom if” that would otherwise wrap the rest of the action.

Which One Should You Use?

  • Just need a boolean? Use exists() (or doesntExist()). It’s the cheapest.
  • Need the row next? Use first() and check for null, or firstOrFail() if missing is a 404.
  • Need the count itself? Use count().
  • About to insert-or-update? Skip the check entirely — use updateOrCreate or firstOrCreate.

Troubleshooting

Call to Undefined Method exists()

If you get Call to undefined method exists(), you’re probably calling it on a model instance instead of a query builder. Make sure there’s a where() clause (or another query method) between the model and exists() — it’s a query-builder method, not an Eloquent instance method.

Race Condition Between Exists Check and Insert

If you’re doing if (!exists) insert(), there’s a window where two requests can both pass the check and both try to insert — causing a duplicate key error on the second. Use updateOrCreate or firstOrCreate instead. These rely on the database’s unique index to resolve the race atomically.

Frequently Asked Questions

What’s the difference between exists() and count() in Laravel?

<code>exists()</code> runs <code>SELECT EXISTS(…)</code> and stops at the first matching row, returning a boolean. <code>count()</code> scans all matching rows to return the total. Use <code>exists()</code> when you only need a yes/no answer; use <code>count()</code> only when the number itself matters.

Is there an opposite of exists() in Laravel?

Yes — <code>doesntExist()</code>. It returns <code>true</code> when no matching record is found, which reads more naturally than <code>!$query->exists()</code> in negative branches.

Should I use first() or exists() to check if a record exists?

Use <code>exists()</code> if you only need the boolean — it’s cheaper because it doesn’t hydrate a model. Use <code>first()</code> if you’re going to use the row anyway. Don’t call both: running <code>exists()</code> then <code>first()</code> is two queries for information one call could have given you.

When should I use firstOrFail() instead of first()?

Use <code>firstOrFail()</code> in controllers where a missing record should return a 404. It throws <code>ModelNotFoundException</code>, which Laravel’s exception handler converts to a 404 response automatically — so you can skip the null check and write cleaner code.

Related Guides

  • Laravel updateOrCreate: Insert or Update Records in Eloquent
  • How to Install Laravel
  • How to Install Composer on Ubuntu

For the full query-builder API, see the official Laravel query builder documentation.

TAGGED:EloquentLaravelmysqlphp

Sign Up For Daily Newsletter

Be keep up! Get the latest breaking news delivered straight to your inbox.
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 updateOrCreate method shown in an Eloquent code snippet with insert and update branches Laravel updateOrCreate: Insert or Update Records in Eloquent
Next Article Laravel validator exists rule checking a post_id against the posts table How to Use the Laravel Validator Exists Rule
Leave a Comment

Leave a Reply Cancel reply

You must be logged in to post a comment.

FacebookLike
XFollow
PinterestPin
InstagramFollow

Subscribe Now

Subscribe to our newsletter to get our newest articles instantly!
Most Popular
Laravel user password being updated via artisan tinker with Hash::make
How to Change a User Password in Laravel
April 19, 2026
Deleting a Laravel user cascades to remove related posts, photos, and notifications
How to Delete Related Records in Laravel Eloquent
April 19, 2026
Laravel migration converting a MySQL column type from VARCHAR to DECIMAL without losing data
How to Change a MySQL Column Type in Laravel Migration
April 19, 2026
Laravel migration adding two new columns to an existing transactions table
How to Add New Columns to an Existing Table in Laravel Migration
April 19, 2026
Laravel foreign key constraint linking posts.user_id to users.id in a schema diagram
How to Add Foreign Keys in Laravel Migration
April 19, 2026

You Might Also Like

Install MySQL on Ubuntu 22.04 — terminal with apt command and database cylinder icon
Server Management

How to Install MySQL on Ubuntu 22.04: Step-by-Step Guide

9 Min Read
Check if GD library is installed in PHP (phpinfo and extension_loaded)
Web Development

How to Check if GD Library Is Installed in PHP (3 Easy Methods)

5 Min Read
Laravel updateOrCreate method shown in an Eloquent code snippet with insert and update branches
Web Development

Laravel updateOrCreate: Insert or Update Records in Eloquent

8 Min Read
Replace Broken Images Automatically with JavaScript
Web Development

Replace Broken Images Automatically with JavaScript (and jQuery)

5 Min Read
How7o

We provide tips, tricks, and advice for improving websites and doing better search.

Latest News

  • SEO Audit Tool
  • Client ReferralsNew
  • Execution of SEO
  • Reporting Tool

Resouce

  • Google Search Console
  • Google Keyword Planner
  • Google OptimiseHot
  • SEO Spider

Get the Top 10 in Search!

Looking for a trustworthy service to optimize the company website?
Request a Quote
Welcome Back!

Sign in to your account

Username or Email Address
Password

Lost your password?