Skip to content

Count Vs Exists (ARCH7063)

Key php.orm.count-vs-exists · Severity info · Category orm

Using count() > 0 to test whether a row exists forces the database to count every match. A cheaper exists() short-circuits at the first row.

<?php
if (User::where('email', $email)->count() > 0) {
// counts every match
}
<?php
if (User::where('email', $email)->exists()) {
// stops at the first row
}

Use exists() for presence checks instead of count() > 0.