What is the best way to check if an Eloquent query returns no answer?

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company
# How to Check if an Eloquent Query Returns No Answer: The Developer's Guide As developers working with relational databases through an ORM like Eloquent, one of the most common hurdles is determining whether a search operation successfully found a record or returned nothing. When you execute a query like `Patient::where('name', '=', 'Bob')`, how do you reliably check if the resulting object (`$patient`) actually exists? Simply checking if `$patient` is `null` can be tricky, especially when dealing with different Eloquent methods. This post will dive into the most robust and efficient ways to determine if an Eloquent query yields zero results, moving beyond simple type checking to implement true best practices. ## The Pitfall of Direct Retrieval Let’s start with your example scenario: ```php $patient = Patient::where('name', '=', 'Bob')->first(); // If Bob does not exist, $patient will be null. ``` If you use methods like `first()`, `find()`, or `firstOrFail()`, checking for `null` is a valid approach: ```php if ($patient) { echo "Patient found!"; } else { echo "Patient not found."; } ``` While functional, this method forces Laravel to execute the query and hydrate the model object before you can check its existence. For scenarios where you *only* need to verify presence—and not necessarily retrieve the full data immediately—there are far more efficient methods available. ## Method 1: The Most Efficient Check – Using `exists()` The absolute best way to check if a query returns any results is by using the `exists()` method on the Eloquent query builder. This method tells the database to execute the most minimal operation required to determine existence, avoiding the overhead of fetching entire model objects. If you only care *if* a record exists, not *what* that record is, this is your go-to solution. ```php $hasBob = Patient::where('name', '=', 'Bob')->exists(); if ($hasBob) { echo "Bob exists in the database."; } else { echo "Bob was not found."; } ``` This approach is significantly more performant, especially when dealing with large tables. It delegates the existence check directly to the underlying SQL engine, making it a highly optimized pattern for data validation within Laravel applications. This principle of efficiency is central to building fast and scalable APIs, much like the design philosophy behind robust frameworks such as [laravelcompany.com](https://laravelcompany.com). ## Method 2: Checking the Count (For Specific Contexts) Another viable method is to use the `count()` method. You can explicitly check if the result count is zero. This is useful when you need both the existence check and the total number of matching records, though it involves slightly more database interaction than `exists()`. ```php $count = Patient::where('name', '=', 'Bob')->count(); if ($count > 0) { echo "Found {$count} record(s)."; } else { echo "No records found."; } ``` While this works perfectly, remember that if you only need a boolean answer (yes/no), `exists()` is generally preferred over counting the results. ## Method 3: Using `firstOrFail()` for Exception Handling For scenarios where *not* finding a record should be treated as an error—for instance, when fetching a specific resource that must exist—Laravel offers `firstOrFail()`. Instead of returning `null` upon failure, this method throws a `ModelNotFoundException`, which allows you to handle the missing data gracefully using exception handling (like route model binding or try-catch blocks). ```php try { $patient = Patient::where('name', '=', 'Bob')->firstOrFail(); // If execution reaches here, $patient is guaranteed to be loaded. echo "Successfully retrieved patient: " . $patient->name; } catch (\Illuminate\Database\Eloquent\ModelNotFoundException $e) { // Handle the case where the record does not exist echo "Error: Patient with that name could not be found."; } ``` ## Conclusion: Choosing the Right Tool In summary, the "best" way to check for no answers depends entirely on your goal: 1. **For pure existence checking (most efficient):** Use `->exists()`. 2. **For retrieving a single model and throwing an error if missing:** Use `->firstOrFail()`. 3. **For retrieving the actual data (if you need it anyway):** Use `->first()` or `->get()`, followed by a standard `if ($model)`. By choosing the method that aligns with your performance needs—favoring existence checks over full data fetches when possible—you ensure your Laravel applications are not only functional but also highly performant.