{"record":{"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/e0f6eb3518ac29fbbca8529e97d0df7fc9f24481/src/Illuminate/Database/Concerns/BuildsQueries.php#L366-L402","documentation":"RecordNotFoundException from firstOrFail() when the query returns no rows. firstOrFail is the strict variant of first(): it returns the single result or throws, used to enforce that an expected record exists (often inside controllers before responding 404).","triggerScenarios":"Model::where('slug', $slug)->firstOrFail() when no row matches; firstOrFail on a filtered query in a show() action with a missing/typo'd id; chain after whereHas that yields nothing.","commonSituations":"User requests a resource that was deleted; wrong route-model-binding key; filter conditions too restrictive; race where a record is removed between check and fetch.","solutions":["Confirm the lookup value is correct and the row exists in the DB.","Provide a custom message: firstOrFail(['*'], 'Post not found') for clearer errors.","If absence is legitimate, use first() and handle null instead of firstOrFail().","Use route-model-binding with explicit key: Route::get('posts/{post:slug}') to fail at the routing layer with 404."],"exampleFix":"// before\n$post = Post::where('slug', $slug)->firstOrFail();\n// after - clearer message\n$post = Post::where('slug', $slug)->firstOrFail(['*'], \"Post [$slug] not found\");\n// or accept absence\n$post = Post::where('slug', $slug)->first();\nabort_unless($post, 404);","handlingStrategy":"try-catch","validationCode":"// Optional pre-check using exists() to give a clearer error or branch.\nif (! Model::where('slug', $slug)->exists()) {\n    abort(404, \"Resource [$slug] not found\");\n}\n$model = Model::where('slug', $slug)->firstOrFail(['*'], \"Resource [$slug] not found\");","typeGuard":"function recordExists(\\Illuminate\\Database\\Eloquent\\Builder $q): bool\n{\n    return $q->exists();\n}","tryCatchPattern":"use Illuminate\\Database\\RecordNotFoundException;\n\ntry {\n    $model = Model::where('slug', $slug)->firstOrFail(['*'], 'Post not found');\n} catch (RecordNotFoundException $e) {\n    abort(404, $e->getMessage());\n}","preventionTips":["Use route-model-binding with implicit key constraints so missing records 404 at the routing layer.","Pass a descriptive custom message to firstOrFail().","If absence is a valid state, use first() and handle null instead of firstOrFail()."],"tags":["database","query-builder","eloquent","not-found"],"backgroundTag":null,"analyzedSha":"e0f6eb3518ac29fbbca8529e97d0df7fc9f24481","analyzedAt":"2026-08-11T20:52:37.562Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}