{"record":{"id":"e6570e8c8b1e3935","repo":"laravel/framework","slug":"you-must-specify-an-orderby-clause-when-using-this","errorCode":null,"errorMessage":"You must specify an orderBy clause when using this function.","messagePattern":"You must specify an orderBy clause when using this function\\.","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"src/Illuminate/Database/Query/Builder.php","lineNumber":3811,"sourceCode":"            yield from $this->connection->cursor(\n                $this->toSql(), $this->getBindings(), ! $this->useWritePdo, $this->fetchUsing\n            );\n        }))->map(function ($item) {\n            return $this->applyAfterQueryCallbacks(new Collection([$item]))->first();\n        })->reject(fn ($item) => is_null($item));\n    }\n\n    /**\n     * Throw an exception if the query doesn't have an orderBy clause.\n     *\n     * @return void\n     *\n     * @throws \\RuntimeException\n     */\n    protected function enforceOrderBy()\n    {\n        if (empty($this->orders) && empty($this->unionOrders)) {\n            throw new RuntimeException('You must specify an orderBy clause when using this function.');\n        }\n    }\n\n    /**\n     * Get a collection instance containing the values of a given column.\n     *\n     * @param  \\Illuminate\\Contracts\\Database\\Query\\Expression|string  $column\n     * @param  string|null  $key\n     * @return \\Illuminate\\Support\\Collection<array-key, mixed>\n     */\n    public function pluck($column, $key = null)\n    {\n        $original = $this->columns;\n\n        // First, we will need to select the results of the query accounting for the\n        // given columns / key. Once we have the results, we will be able to take\n        // the results and get the exact data that was requested for the query.\n        $this->columns ??= is_null($key) || $key === $column","sourceCodeStart":3793,"sourceCodeEnd":3829,"githubUrl":"https://github.com/laravel/framework/blob/e0f6eb3518ac29fbbca8529e97d0df7fc9f24481/src/Illuminate/Database/Query/Builder.php#L3793-L3829","documentation":"enforceOrderBy() is called by methods whose result is only meaningful with a deterministic row order — forPage(), skip()/take() with an offset, and a few cursor-style helpers. If neither $this->orders nor $this->unionOrders is populated, it throws RuntimeException, because paginating an unordered query returns arbitrary (and unstable) rows.","triggerScenarios":"->forPage(2, 15) or ->skip(10)->take(5) on a query that has no prior orderBy() call.","commonSituations":"Adding pagination to an existing query and forgetting the order; relying on default insertion order which DBs do not guarantee; paginating a query that previously used an index but now does a full scan.","solutions":["Add ->orderBy('id') (or another deterministic column) before paginate()/forPage()/skip().","Order by a unique, monotonic column to make pagination stable across pages.","If you truly want unordered rows, fetch without skip/take."],"exampleFix":"// before\nUser::forPage($page, $size)->get();\n\n// after\nUser::orderBy('id')->forPage($page, $size)->get();","handlingStrategy":"validation","validationCode":"if (empty($query->orders) && empty($query->unionOrders)) {\n    $query->orderBy('id');\n}\n$query->forPage($page, $size);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always call orderBy() on a unique/monotonic column before paginate()/forPage()/skip().","Add a repository-level convention that all paged queries start with a deterministic order.","Test pagination with a seeded dataset to catch missing orderBy early."],"tags":["query-builder","pagination","order-by","database"],"backgroundTag":null,"analyzedSha":"e0f6eb3518ac29fbbca8529e97d0df7fc9f24481","analyzedAt":"2026-08-11T20:52:37.562Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}