Laravel 9执行数据库迁移提示类名已被使用错误排查
问题描述
删除Laravel框架自带的users迁移文件后,自行创建新的用户表迁移,多次调整后问题仍未解决。最初设置的迁移类名为CreateUsersTable,后续修改为CreateUserTable,期间尝试过删除数据库重建、清理Laravel所有缓存,问题依旧存在,不确定Laravel在何处缓存了1-2小时前已删除的旧迁移文件信息,删除文件后仍会被识别加载。
报错信息
Illuminate\Database\QueryException C:\xampp\htdocs\chikchika>php artisan migrate Migration table created successfully. Migrating: 2014_10_12_100000_create_password_resets_table Migrated: 2014_10_12_100000_create_password_resets_table (9.05ms) Migrating: 2019_08_19_000000_create_failed_jobs_table Migrated: 2019_08_19_000000_create_failed_jobs_table (11.17ms) Migrating: 2019_12_14_000001_create_personal_access_tokens_table Migrated: 2019_12_14_000001_create_personal_access_tokens_table (24.01ms) Migrating: 2022_06_21_083427_followers Migrated: 2022_06_21_083427_followers (6.22ms) Migrating: 2022_06_21_084259_email_verification Migrated: 2022_06_21_084259_email_verification (6.40ms) Migrating: 2022_06_21_084605_search_keywords Migrated: 2022_06_21_084605_search_keywords (7.15ms) Migrating: 2022_06_21_084834_dm_conversition Migrated: 2022_06_21_084834_dm_conversition (7.65ms) Migrating: 2022_06_21_085432_dm_messages Migrated: 2022_06_21_085432_dm_messages (6.13ms) Migrating: 2022_06_21_085828_posts Migrated: 2022_06_21_085828_posts (5.89ms) Migrating: 2022_06_21_090526_post_files Migrated: 2022_06_21_090526_post_files (6.21ms) Migrating: 2022_06_21_090756_post_reactions Migrated: 2022_06_21_090756_post_reactions (6.63ms) Migrating: 2022_06_21_091042_comments Migrated: 2022_06_21_091042_comments (7.98ms) Migrating: 2022_06_21_092616_comment_reactions Migrated: 2022_06_21_092616_comment_reactions (6.44ms) Migrating: 2022_06_21_092807_system Migrated: 2022_06_21_092807_system (11.89ms) PHP Fatal error: Cannot declare class CreateUsersTable, because the name is already in use in C:\xampp\ htdocs\chikchika\database\migrations\2022_06_21_124328_users.php on line 7 Symfony\Component\ErrorHandler\Error\FatalError Cannot declare class CreateUsersTable, because the name is already in use at C:\xampp\htdocs\chikchika\database\migrations\2022_06_21_124328_users.php:7 3▕ use Illuminate\Database\Migrations\Migration; 4▕ use Illuminate\Database\Schema\Blueprint; 5▕ use Illuminate\Support\Facades\Schema; 6▕ ➜ 7▕ class CreateUsersTable extends Migration 8▕ { 9▕ /** 10▕ * Run the migrations. 11▕ * Whoops\Exception\ErrorException Cannot declare class CreateUsersTable, because the name is already in use at C:\xampp\htdocs\chikchika\database\migrations\2022_06_21_124328_users.php:7 3▕ use Illuminate\Database\Migrations\Migration; 4▕ use Illuminate\Database\Schema\Blueprint; 5▕ use Illuminate\Support\Facades\Schema; 6▕ ➜ 7▕ class CreateUsersTable extends Migration 8▕ { 9▕ /** 10▕ * Run the migrations. 11▕ * 1 C:\xampp\htdocs\chikchika\vendor\filp\whoops\src\Whoops\Run.php:486 Whoops\Run::handleError("Cannot declare class CreateUsersTable, because the name is already in use ", "C:\xampp\htdocs\chikchika\database\migrations\2022_06_21_124328_users.php") 2 [internal]:0 Whoops\Run::handleShutdown() C:\xampp\htdocs\chikchika>php artisan migrate PHP Fatal error: Cannot declare class CreateUserTable, because the name is already in use in C:\xampp\h tdocs\chikchika\database\migrations\2022_06_21_124328_users.php on line 7 Symfony\Component\ErrorHandler\Error\FatalError Cannot declare class CreateUserTable, because the name is already in use at C:\xampp\htdocs\chikchika\database\migrations\2022_06_21_124328_users.php:7 3▕ use Illuminate\Database\Migrations\Migration; 4▕ use Illuminate\Database\Schema\Blueprint; C:\xampp\htdocs\chikchika>php artisan migrate PHP Fatal error: Cannot declare class CreateUserTable, because the name is already in use in C:\xampp\htdocs\chikchika\database\migrations\2022_06_21_124328_user.php on line 7 Symfony\Component\ErrorHandler\Error\FatalError Cannot declare class CreateUserTable, because the name is already in use at C:\xampp\htdocs\chikchika\database\migrations\2022_06_21_124328_user.php:7 3▕ use Illuminate\Database\Migrations\Migration; 4▕ use Illuminate\Database\Schema\Blueprint; 5▕ use Illuminate\Support\Facades\Schema; 6▕ ➜ 7▕ class CreateUserTable extends Migration 8▕ { 9▕ /** 10▕ * Run the migrations. 11▕ * Whoops\Exception\ErrorException Cannot declare class CreateUserTable, because the name is already in use at C:\xampp\htdocs\chikchika\database\migrations\2022_06_21_124328_user.php:7 3▕ use Illuminate\Database\Migrations\Migration; 4▕ use Illuminate\Database\Schema\Blueprint; 5▕ use Illuminate\Support\Facades\Schema; 6▕ ➜ 7▕ class CreateUserTable extends Migration 8▕ { 9▕ /** 10▕ * Run the migrations. C:\xampp\htdocs\chikchika>php artisan migrate PHP Fatal error: Cannot declare class CreateUserTable, because the name is already in use in C:\xampp\htdocs\chikchika\database\migrations\2022_06_21_124328_user.php on line 7 Symfony\Component\ErrorHandler\Error\FatalError Cannot declare class CreateUserTable, because the name is already in use at C:\xampp\htdocs\chikchika\database\migrations\2022_06_21_124328_user.php:7 3▕ use Illuminate\Database\Migrations\Migration; 4▕ use Illuminate\Database\Schema\Blueprint; 5▕ use Illuminate\Support\Facades\Schema; 6▕ ➜ 7▕ class CreateUserTable extends Migration 8▕ { 9▕ /** 10▕ * Run the migrations. 11▕ * Whoops\Exception\ErrorException Cannot declare class CreateUserTable, because the name is already in use at C:\xampp\htdocs\chikchika\database\migrations\2022_06_21_124328_user.php:7 3▕ use Illuminate\Database\Migrations\Migration; 4▕ use Illuminate\Database\Schema\Blueprint; 5▕ use Illuminate\Support\Facades\Schema; 6▕ ➜ 7▕ class CreateUserTable extends Migration 8▕ { 9▕ /** 10▕ * Run the migrations. 11▕ * 1 C:\xampp\htdocs\chikchika\vendor\filp\whoops\src\Whoops\Run.php:486 Whoops\Run::handleError("Cannot declare class CreateUserTable, because the name is already in use", "C:\xampp\htdocs\chikchika\database\migrations\2022_06_21_124328_user.php") 2 [internal]:0 Whoops\Run::handleShutdown() C:\xampp\htdocs\chikchika>
当前使用的迁移文件代码
当前使用的2022_06_21_124328_user.php文件代码如下:
<?php use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; class CreateUserTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('users', function (Blueprint $table) { $table->id(); $table->string('username'); $table->string('name'); $table->string('email')->unique(); $table->timestamp('email_verified_at')->nullable(); $table->string('password'); $table->timestamp('account_verified')->nullable(); $table->integer('level')->default(0); // 0 - member, 1 - moder, 2 - admin $table->string('bio')->nullable(); $table->string('avatar')->nullable(); $table->string('cover')->nullable(); $table->integer('audience')->default(0); // 0 - public, 1 - private $table->timestamp('birth_date')->nullable(); $table->string('location')->nullable(); $table->string('website')->nullable(); $table->integer('disabled')->default(0); $table->rememberToken(); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('users'); } }
问题原因
这个报错和Laravel缓存无关,本质是PHP类重复声明错误:
- 执行
php artisan migrate时,Laravel会自动加载database/migrations目录下所有PHP文件(包括子目录内的文件)。你在反复修改迁移文件名、类名的过程中,目录里残留了未删除干净的旧迁移文件,两个不同的文件中定义了同名的迁移类,PHP加载时就会抛出类名已被使用的致命错误。 - 清理Laravel缓存无效,是因为Laravel的应用缓存、迁移缓存都不存储迁移类的声明代码,类重复是文件系统层面存在重复类定义导致的,和缓存机制没有关系。
解决步骤
- 打开项目
database/migrations目录,全局搜索CreateUsersTable和CreateUserTable两个字符串,把所有定义了这两个类的多余文件全部删除,确保整个migrations目录(含所有子目录)下只有一个用户表迁移文件,且类名全局唯一。 - 按照Laravel迁移命名规范调整文件和类名:创建users表的迁移文件名建议统一为
2022_06_21_124328_create_users_table.php,文件内的类名对应改为CreateUsersTable,避免后续类自动加载出现匹配问题。 - 在项目根目录执行
composer dump-autoload重新生成类自动加载映射,不需要额外执行其他缓存清理命令,之后重新运行php artisan migrate即可正常执行迁移。
内容的提问来源于stack exchange,提问作者WEB TM
相关产品推荐
相关产品推荐

