{"id":"766d95b207a3032d","repo":"laravel/framework","slug":"index-name-contains-invalid-characters-766d95","errorCode":null,"errorMessage":"Index name contains invalid characters.","messagePattern":"Index name contains invalid characters\\.","errorType":"exception","errorClass":"InvalidArgumentException","httpStatus":null,"severity":"error","filePath":"src/Illuminate/Database/Query/Grammars/SqlServerGrammar.php","lineNumber":126,"sourceCode":"    /**\n     * Compile the index hints for the query.\n     *\n     * @param  \\Illuminate\\Database\\Query\\Builder  $query\n     * @param  \\Illuminate\\Database\\Query\\IndexHint  $indexHint\n     * @return string\n     *\n     * @throws \\InvalidArgumentException\n     */\n    protected function compileIndexHint(Builder $query, $indexHint)\n    {\n        if ($indexHint->type !== 'force') {\n            return '';\n        }\n\n        $index = $indexHint->index;\n\n        if (! preg_match('/^[a-zA-Z0-9_$]+$/', $index)) {\n            throw new InvalidArgumentException('Index name contains invalid characters.');\n        }\n\n        return \"with (index([{$index}]))\";\n    }\n\n    /**\n     * {@inheritdoc}\n     *\n     * @param  \\Illuminate\\Database\\Query\\Builder  $query\n     * @param  array  $where\n     * @return string\n     */\n    protected function whereBitwise(Builder $query, $where)\n    {\n        $value = $this->parameter($where['value']);\n\n        $operator = str_replace('?', '??', $where['operator']);\n","sourceCodeStart":108,"sourceCodeEnd":144,"githubUrl":"https://github.com/laravel/framework/blob/bd6b5437e6ad87bb49f9b426724f07a9f64e9683/src/Illuminate/Database/Query/Grammars/SqlServerGrammar.php#L108-L144","documentation":"SqlServerGrammar::compileIndexHint() throws InvalidArgumentException when a force-type hint's index name fails the regex /^[a-zA-Z0-9_$]+$/. SQL Server compiles forceIndex to WITH (INDEX([...])) and only supports the force type; use/ignore hints return empty string and are not validated.","triggerScenarios":"Calling forceIndex($name) on a sqlsrv connection where $name contains characters outside [a-zA-Z0-9_$] (hyphen, space, dot, brackets). useIndex/ignoreIndex do not throw on SQL Server.","commonSituations":"Porting MySQL index-hint code to SQL Server where the index name has a different format. Using bracketed/qualified index identifiers that contain invalid characters.","solutions":["Pass the bare SQL Server index name containing only [a-zA-Z0-9_$].","Guard forceIndex behind a driver check and skip it for SQL Server if not essential.","Rename the index to use underscore-safe characters."],"exampleFix":"// before\n$query->from('users')->forceIndex('idx.active.users');\n\n// after\n$query->from('users')->forceIndex('idx_active_users');","handlingStrategy":"validation","validationCode":"// SQL Server only validates force-type hints\nif ($query->getConnection()->getDriverName() === 'sqlsrv' && $hintType === 'force') {\n    if (! preg_match('/^[a-zA-Z0-9_$]+$/', $index)) {\n        throw new \\InvalidArgumentException(\"Invalid SQL Server index name: {$index}\");\n    }\n}\n$query->from('users')->forceIndex($index);","typeGuard":"function isValidSqlServerIndexName(string $name): bool\n{\n    return (bool) preg_match('/^[a-zA-Z0-9_$]+$/', $name);\n}","tryCatchPattern":null,"preventionTips":["Avoid bracketed/dotted identifiers in forceIndex names on SQL Server.","Standardise on [a-zA-Z0-9_$] index names across engines.","Skip forceIndex on SQL Server when portability matters more than the hint."],"tags":["index-hint","sqlsrv","validation","invalid-characters"],"analyzedSha":"bd6b5437e6ad87bb49f9b426724f07a9f64e9683","analyzedAt":"2026-08-06T00:28:32.783Z","schemaVersion":2}