Replace where('column', $val)->first() chains with the concise firstWhere() query builder method.
Looking up a single model by email, slug, or token using Model::where('email', $email)->first() is one of the most frequent query patterns in Laravel applications.
The firstWhere() method combines where() and first() into a single call.
Basic Usage
use App\Models\User;
// Before: Verbose two-step query
$user = User::where('email', $email)->first();
// After: Clean single-method query
$user = User::firstWhere('email', $email);
With Comparison Operators
// Retrieve the first order over $1,000
$highValueOrder = Order::firstWhere('total_amount', '>', 1000);
Works on Eloquent Collections Too
firstWhere() works identically on in-memory Eloquent and Support Collections:
$users = User::all();
// Filters collection in memory without executing a new SQL query
$admin = $users->firstWhere('role', 'admin');
Summary
- Shorthand for
->where(...)->first(). - Available on both Eloquent Query Builders and Collections.
- Supports operator arguments (
firstWhere('col', '>', $val)).
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.