Skip to main content
Laravel Output Prediction Hard +35 XP

firstOrFail vs find

What happens when firstOrFail() finds no record?

LaravelRead-only
$user = User::where('email', 'unknown@test.com')->firstOrFail();

What is the output?

Show answer and explanation

Answer: Throws ModelNotFoundException (404)

firstOrFail() throws ModelNotFoundException if no record is found, which Laravel automatically converts to a 404 HTTP response.

Hint: fail = exception, not null.

What this challenge tests

Output Prediction in Laravel: eloquent, exceptions.

Related challenges