BACK
Laravel / Eloquent

Use sole() Instead of firstOrFail() for Single Record Guarantees

Punyapal Shah 1 min read
edit this tip

When you expect exactly one matching record, use sole() instead of firstOrFail(). It guards against multiple records by throwing MultipleRecordsFoundException.

When querying unique records, firstOrFail() silently returns the first record even if multiple records match due to data integrity issues. sole() makes sure exactly one record exists.

// Throws ModelNotFoundException if 0, MultipleRecordsFoundException if 2+
$user = User::where('verification_token', $token)->sole();
  • Asserts that exactly one record matches criteria
  • Catches data integrity anomalies before bad states propagate
  • Throws explicit MultipleRecordsFoundException

Related Tips

View all tips →

// Got a tip in mind? Contributions are always welcome