How7o
  • Home
  • Tools
  • Prank Screens
  • Learn
  • Blog
  • Contact
Reading: How to Check if a Record Exists in Laravel
Share
How7oHow7o
Font ResizerAa
  • OS
Search
  • Home
  • Tools
  • Prank Screens
  • Learn
  • Blog
  • Contact
Follow US
© 2024–2026 How7o. All rights reserved.
How7o > Learn > 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.
[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 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
Most Popular
Display PHP errors — ini_set + php.ini configuration
How to Display PHP Errors
May 10, 2026
PHP convert string to uppercase — strtoupper and mb_strtoupper
How to Convert a String to Uppercase in PHP
May 10, 2026
PHP string to float conversion with cast, regex cleanup, NumberFormatter
How to Convert a String to Float in PHP
May 10, 2026
PHP merge arrays without duplicates — union operator and array_unique
How to Combine Two Arrays Without Duplicates in PHP
May 10, 2026
PHP delete array element — unset, array_splice, array_filter, array_search
How to Delete an Element from a PHP Array
May 10, 2026

You Might Also Like

Install Laravel on Ubuntu — terminal with composer create-project command and Laravel red-pillar icon
Web Development

How to Install Laravel on Ubuntu: Step-by-Step Guide

9 Min Read
CSS page break for printing shown in a print preview layout
Web Development

CSS Page Break for Printing: How to Split a Web Page Into Multiple Printed Pages

6 Min Read
MySQL extract digits from string — REGEXP_REPLACE negation class
Web Development

How to Extract Only the Digits from a String in MySQL

6 Min Read
Laravel foreign key constraint linking posts.user_id to users.id in a schema diagram
Web Development

How to Add Foreign Keys in Laravel Migration

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?