{"record":{"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/e0f6eb3518ac29fbbca8529e97d0df7fc9f24481/src/Illuminate/Database/Migrations/MigrationCreator.php#L91-L127","documentation":"MigrationCreator::ensureMigrationDoesntAlreadyExist guards against duplicate migration class names. After optionally require_once-ing the existing migration files in the target path, it calls class_exists() on the StudlyCase class name derived from the migration name; if that class is already declared in the PHP runtime, creation aborts with InvalidArgumentException. The check is global to the process, so a class loaded from any source counts as a collision.","triggerScenarios":"Calling MigrationCreator::create($name, $path) — or `php artisan make:migration <name>` — where the derived class name (e.g. CreateUsersTable for name 'create_users_table') is already loaded. The collision can come from a same-named file in migrationPath, or from a class loaded earlier in the same PHP process.","commonSituations":"Re-running make:migration with the same name after a typo fix; two migration files mapping to the same Studly class name; migrations already required by a test bootstrap or an earlier artisan command in the same process; an old file left in place while recreating it.","solutions":["Choose a more specific migration name (e.g. add_status_to_users_table) so the derived class name is unique.","Delete or rename the pre-existing migration file that owns the colliding class name, then re-run.","If the class is only resident because a previous command kept it in memory, re-run make:migration in a fresh PHP process."],"exampleFix":"// before\nphp artisan make:migration create_users_table\n\n// after\nphp artisan make:migration add_status_to_users_table --table=users","handlingStrategy":"validation","validationCode":"// Before calling MigrationCreator::create(), derive the class name the same way and check it.\n$className = Str::studly(preg_replace('/^(\\d{4}_\\d{2}_\\d{2}_)?/', '', $name)); // mimic getClassName\nif (class_exists($className)) {\n    throw new RuntimeException(\"Migration class {$className} already exists; pick a new name.\");\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Adopt a naming convention that includes intent (add_X, create_Y, drop_Z) so collisions are unlikely.","Delete obsolete migration files rather than recreating them with the same name.","Run make:migration in a fresh PHP process in long-running tools (e.g. test runners) to avoid stale class declarations."],"tags":["migrations","artisan","database","class-collision"],"backgroundTag":null,"analyzedSha":"e0f6eb3518ac29fbbca8529e97d0df7fc9f24481","analyzedAt":"2026-08-11T20:52:37.562Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}