{"id":"a75ee82dea16839e","repo":"laravel/framework","slug":"no-record-found-for-the-given-query","errorCode":null,"errorMessage":"No record found for the given query.","messagePattern":"No record found for the given query\\.","errorType":"exception","errorClass":"RecordNotFoundException","httpStatus":null,"severity":"error","filePath":"src/Illuminate/Database/Concerns/BuildsQueries.php","lineNumber":384,"sourceCode":"        return $this->limit(1)->get($columns)->first();\n    }\n\n    /**\n     * Execute the query and get the first result or throw an exception.\n     *\n     * @param  array|string  $columns\n     * @param  string|null  $message\n     * @return TValue\n     *\n     * @throws \\Illuminate\\Database\\RecordNotFoundException\n     */\n    public function firstOrFail($columns = ['*'], $message = null)\n    {\n        if (! is_null($result = $this->first($columns))) {\n            return $result;\n        }\n\n        throw new RecordNotFoundException($message ?: 'No record found for the given query.');\n    }\n\n    /**\n     * Execute the query and get the first result if it's the sole matching record.\n     *\n     * @param  array|string  $columns\n     * @return TValue\n     *\n     * @throws \\Illuminate\\Database\\RecordsNotFoundException\n     * @throws \\Illuminate\\Database\\MultipleRecordsFoundException\n     */\n    public function sole($columns = ['*'])\n    {\n        $result = $this->limit(2)->get($columns);\n\n        $count = $result->count();\n\n        if ($count === 0) {","sourceCodeStart":366,"sourceCodeEnd":402,"githubUrl":"https://github.com/laravel/framework/blob/bd6b5437e6ad87bb49f9b426724f07a9f64e9683/src/Illuminate/Database/Concerns/BuildsQueries.php#L366-L402","documentation":"Thrown by Builder::firstOrFail when the query returns no rows. Unlike first() (which returns null), firstOrFail is the explicit 'I expect exactly one row' API and raises RecordNotFoundException (a subclass of RuntimeException's ModelNotFoundException family) so callers can distinguish 'not found' from null. The message defaults to 'No record found for the given query.' but can be customised via the second argument.","triggerScenarios":"User::where('email', $email)->firstOrFail() where no user has that email; route-model binding that falls through to firstOrFail; findOrFail equivalent via firstOrFail; using firstOrFail in a controller to enforce existence.","commonSituations":"Lookup by unique field where the value is missing; deleted/archived records filtered out by a global scope; wrong tenant/scope scoping the query to nothing; user-supplied ID that does not exist; soft-deletes hiding the row.","solutions":["Use first() if 'not found' is a valid business outcome and handle null explicitly.","Catch Illuminate\\Database\\RecordNotFoundException (or ModelNotFoundException) to render a 404.","Verify filters/scopes: check soft-deletes (use withTrashed()), tenant scope, and active scopes.","Provide a clearer message: ->firstOrFail(['*'], 'User not found for email '.$email)."],"exampleFix":"// before\n$user = User::where('email', $email)->firstOrFail();\n// throws generic message\n\n// after\nuse Illuminate\\Database\\RecordNotFoundException;\n\ntry {\n    $user = User::where('email', $email)->firstOrFail(['*'], 'No user with that email.');\n} catch (RecordNotFoundException $e) {\n    abort(404, $e->getMessage());\n}","handlingStrategy":"try-catch","validationCode":"$row = Model::where('email', $email)->first();\nif ($row === null) {\n    abort(404, 'Not found');\n}","typeGuard":"function rowExists(\\Illuminate\\Database\\Eloquent\\Builder $q): bool { return $q->exists(); }","tryCatchPattern":"use Illuminate\\Database\\RecordNotFoundException;\ntry {\n    $user = User::where('email', $email)->firstOrFail();\n} catch (RecordNotFoundException $e) {\n    abort(404, $e->getMessage());\n}","preventionTips":["Use first() and handle null when 'not found' is expected.","Provide a descriptive custom message to firstOrFail.","Verify scopes (soft-delete, tenant) before the lookup."],"tags":["database","eloquent","not-found","query-builder"],"analyzedSha":"bd6b5437e6ad87bb49f9b426724f07a9f64e9683","analyzedAt":"2026-08-06T00:28:32.783Z","schemaVersion":2}