Use doesntExist() instead of ! exists() to make conditional database checks clean and expressive.
Checking whether records do not exist in the database is a common requirement (such as verifying slug uniqueness or confirming an invite has not been claimed).
Instead of prefixing exists() with an exclamation point (! $query->exists()), Laravel provides the expressive doesntExist() method.
Basic Usage
use App\Models\User;
// Before: Easy to overlook the leading exclamation mark
if (! User::where('email', $email)->exists()) {
// ...
}
// After: Clear and descriptive
if (User::where('email', $email)->doesntExist()) {
// ...
}
Checking Relationships
doesntExist() works on any Eloquent relationship query:
if ($user->orders()->where('status', 'completed')->doesntExist()) {
// User has never completed an order
$user->sendWelcomeDiscountNotification();
}
Aborting on Missing Records with doesntExistOr()
You can also execute a closure or abort when no records exist:
User::where('api_token', $token)->doesntExistOr(function () {
abort(401, 'Invalid API Token.');
});
Summary
- Eliminates easily overlooked
!negation symbols in conditional logic. - Returns a boolean without hydrating model instances into memory.
- Improves code readability in validation and business rule checks.
Related Tips
View all tips →Scope Pivot Table Queries with Closures in wherePivot() in Laravel 13.26
Laravel 13.26 allows wherePivot() and orWherePivot() to accept closures, enabling direct reuse of local scopes defined on custom Pivot models.
Simplify Chronological Query Sorting with latest() and oldest()
Replace verbose orderBy('created_at', 'desc') clauses with the clean latest() and oldest() query builder helpers.