{"id":"03570e4c0c03a01c","repo":"laravel/framework","slug":"order-direction-must-be-a-sortdirection-asc-or","errorCode":null,"errorMessage":"Order direction must be a SortDirection, \"asc\" or \"desc\".","messagePattern":"Order direction must be a SortDirection, \"asc\" or \"desc\"\\.","errorType":"exception","errorClass":"InvalidArgumentException","httpStatus":null,"severity":"error","filePath":"src/Illuminate/Database/Query/Builder.php","lineNumber":2992,"sourceCode":"     */\n    public function orderBy($column, $direction = SortDirection::Ascending)\n    {\n        if ($this->isQueryable($column)) {\n            [$query, $bindings] = $this->createSub($column);\n\n            $column = new Expression('('.$query.')');\n\n            $this->addBinding($bindings, $this->unions ? 'unionOrder' : 'order');\n        }\n\n        $direction = match (true) {\n            $direction instanceof SortDirection => match ($direction) {\n                SortDirection::Ascending => 'asc',\n                SortDirection::Descending => 'desc',\n            },\n            strtolower($direction) === 'asc' => 'asc',\n            strtolower($direction) === 'desc' => 'desc',\n            default => throw new InvalidArgumentException('Order direction must be a SortDirection, \"asc\" or \"desc\".'),\n        };\n\n        $this->{$this->unions ? 'unionOrders' : 'orders'}[] = [\n            'column' => $column,\n            'direction' => $direction,\n        ];\n\n        return $this;\n    }\n\n    /**\n     * Add a descending \"order by\" clause to the query.\n     *\n     * @param  \\Closure|\\Illuminate\\Database\\Query\\Builder|\\Illuminate\\Database\\Eloquent\\Builder<*>|\\Illuminate\\Contracts\\Database\\Query\\Expression|string  $column\n     * @return $this\n     */\n    public function orderByDesc($column)\n    {","sourceCodeStart":2974,"sourceCodeEnd":3010,"githubUrl":"https://github.com/laravel/framework/blob/bd6b5437e6ad87bb49f9b426724f07a9f64e9683/src/Illuminate/Database/Query/Builder.php#L2974-L3010","documentation":"Thrown by orderBy's match expression when $direction is neither a SortDirection enum case nor a string that lowercases to 'asc'/'desc'. Since the enum introduction, only those three normalized values are accepted; typos, localized strings, or uppercase variants with weird casing are caught here (note the code lowercases the string before comparing, so 'ASC' is fine, but 'ascending' or '' is not).","triggerScenarios":"`orderBy('created_at', 'ascending')`. Passing an empty string default. Passing user-controlled input like `$request->input('dir')` directly as direction. Passing a boolean or integer as direction.","commonSituations":"Datatables/sortable-table controllers forwarding `?dir=...` straight into orderBy; i18n code returning translated direction words; refactoring away from magic strings to the enum but missing one call site.","solutions":["Use the enum: `orderBy('col', SortDirection::Descending)` or the helper `orderByDesc('col')`.","Whitelist user input: `$dir = in_array(strtolower($req->dir), ['asc','desc']) ? $req->dir : 'asc';`.","Pass the literal string 'asc' or 'desc'.","Coerce empty/null to a default before calling orderBy."],"exampleFix":"// before\n$query->orderBy('name', $request->input('sort_dir'));\n// when sort_dir=ascending => Order direction must be a SortDirection...\n\n// after\n$dir = in_array(strtolower((string) $request->input('sort_dir')), ['asc','desc'], true)\n    ? strtolower((string) $request->input('sort_dir'))\n    : 'asc';\n$query->orderBy('name', $dir);","handlingStrategy":"validation","validationCode":"use Illuminate\\Database\\Query\\SortDirection;\n\n$dir = match (strtolower((string) $input)) {\n    'asc', 'ascending' => 'asc',\n    'desc', 'descending' => 'desc',\n    default => 'asc',\n};\n$query->orderBy($col, $dir);","typeGuard":"function isValidOrderDirection(mixed $d): bool\n{\n    return $d instanceof \\Illuminate\\Database\\Query\\SortDirection\n        || in_array(strtolower((string) $d), ['asc','desc'], true);\n}","tryCatchPattern":"// Validate the direction from user input before calling orderBy; do not try/catch.","preventionTips":["Forward ?sort_dir through a whitelist of ['asc','desc'] in the controller.","Prefer the SortDirection enum or orderByDesc/orderBy helpers at call sites.","Default missing direction input to 'asc' explicitly."],"tags":["query-builder","order-by","enum","invalid-argument"],"analyzedSha":"bd6b5437e6ad87bb49f9b426724f07a9f64e9683","analyzedAt":"2026-08-06T00:28:32.783Z","schemaVersion":2}