{"id":"2c3643b32821dc6b","repo":"laravel/framework","slug":"a-classname-class-already-exists","errorCode":null,"errorMessage":"A {$className} class already exists.","messagePattern":"A (.+?) class already exists\\.","errorType":"exception","errorClass":"InvalidArgumentException","httpStatus":null,"severity":"error","filePath":"src/Illuminate/Database/Migrations/MigrationCreator.php","lineNumber":109,"sourceCode":"     *\n     * @param  string  $name\n     * @param  string|null  $migrationPath\n     * @return void\n     *\n     * @throws \\InvalidArgumentException\n     */\n    protected function ensureMigrationDoesntAlreadyExist($name, $migrationPath = null)\n    {\n        if (! empty($migrationPath)) {\n            $migrationFiles = $this->files->glob($migrationPath.'/*.php');\n\n            foreach ($migrationFiles as $migrationFile) {\n                $this->files->requireOnce($migrationFile);\n            }\n        }\n\n        if (class_exists($className = $this->getClassName($name))) {\n            throw new InvalidArgumentException(\"A {$className} class already exists.\");\n        }\n    }\n\n    /**\n     * Get the migration stub file.\n     *\n     * @param  string|null  $table\n     * @param  bool  $create\n     * @return string\n     */\n    protected function getStub($table, $create)\n    {\n        if (is_null($table)) {\n            $stub = $this->files->exists($customPath = $this->customStubPath.'/migration.stub')\n                ? $customPath\n                : $this->stubPath().'/migration.stub';\n        } elseif ($create) {\n            $stub = $this->files->exists($customPath = $this->customStubPath.'/migration.create.stub')","sourceCodeStart":91,"sourceCodeEnd":127,"githubUrl":"https://github.com/laravel/framework/blob/bd6b5437e6ad87bb49f9b426724f07a9f64e9683/src/Illuminate/Database/Migrations/MigrationCreator.php#L91-L127","documentation":"Thrown by MigrationCreator::ensureMigrationDoesntAlreadyExist when PHP's class_exists() reports that the migration class name derived from the migration name is already loaded. Laravel derives a ClassName from the snake_case migration name; if two migrations resolve to the same class (or the class exists anywhere in the autoloader) the generator refuses to overwrite. This protects against duplicate migration definitions that would silently collide at runtime.","triggerScenarios":"Calling `php artisan make:migration create_users_table` when a `CreateUsersTable` class already exists in database/migrations (or anywhere autoloaded). Re-running the make command after a failed/half-written run. Naming two migrations such that their StudlyCase derivatives collide (e.g. `add_index_to_users` and `add_index_to_users_table` both could resolve similarly if normalized). Composer dump-autoload leaving a stale classmap pointing at a deleted file.","commonSituations":"Re-scaffolding migrations during TDD; copying a migration file and forgetting to rename the inner class; autoloader holding a cached classmap entry after the file was deleted; running make:migration in a fresh clone before `composer install` populated vendor yet an old class persists in opcache.","solutions":["Rename the new migration on the command line: `php artisan make:migration create_users_table_v2` so the derived class differs.","Search the codebase for the colliding class name (`grep -R \"class CreateUsersTable\"`) and remove or rename the old definition, then run `composer dump-autoload`.","If the duplicate file was deleted but the autoloader still resolves the class, clear APCu/opcache and run `composer dump-autoload -o`.","Delete the half-written migration stub from database/migrations before re-running the make command."],"exampleFix":"// before\nphp artisan make:migration create_users_table\n// => A CreateUsersTable class already exists.\n\n// after\nphp artisan make:migration create_users_table_v2 --create=users","handlingStrategy":"validation","validationCode":"use Illuminate\\Support\\Str;\n\n$className = Str::studly(preg_replace('/\\d{4}_\\d{2}_\\d{2}_\\d{6}_/', '', $migrationName));\nif (class_exists($className)) {\n    throw new RuntimeException(\"Migration class {$className} already exists; pick a new name.\");\n}","typeGuard":"// none: class existence is a runtime autoloader check, not a type narrow.\nfunction migrationClassExists(string $name): bool\n{\n    return class_exists(Str::studly(Str::afterLast($name, '_')));\n}","tryCatchPattern":"// Migration creation is a build-time step; validate/guard the name rather than try/catch.\n// If you must, catch \\InvalidArgumentException in the command runner and prompt for a new name.","preventionTips":["Use a consistent naming convention that includes a purpose suffix to avoid class-name collisions.","Run `composer dump-autoload` after deleting migration files so stale classmap entries are purged.","Never copy a migration file without renaming both the file and the inner class."],"tags":["migrations","artisan","autoloader","class-collision"],"analyzedSha":"bd6b5437e6ad87bb49f9b426724f07a9f64e9683","analyzedAt":"2026-08-06T00:28:32.783Z","schemaVersion":2}