Laravel迁移报错:无法声明CreateUsersTable类,提示名称已占用
Laravel迁移报错:CreateUsersTable类已存在但排查不到重复声明
执行php artisan migrate时触发如下致命错误:
PS C:\Users\joshu\laraveljobs_app> php artisan migrate INFO Running migrations. PHP Fatal error: Cannot declare class CreateUsersTable, because the name is already in use in C:\Users\joshu\laraveljobs_app\database\migrations\create_users_table.php on line 7 Symfony\Component\ErrorHandler\Error\FatalError Cannot declare class CreateUsersTable, because the name is already in use at database\migrations\create_users_table.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 database\migrations\create_users_table.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 vendor\filp\whoops\src\Whoops\Run.php:510 Whoops\Run::handleError("Cannot declare class CreateUsersTable, because the name is already in use", "C:\Users\joshu\laraveljobs_app\database\migrations\create_users_table.php") 2 [internal]:0 Whoops\Run::handleShutdown()
用户已通过findstr /s "CreateUsersTable"命令排查,仅找到一处类声明,但错误依旧。
排查及解决方法
- 检查文件名大小写冲突:Windows系统路径不区分大小写,若
database/migrations目录下存在create_users_table.php和CreateUsersTable.php(仅大小写不同),PHP会加载两次导致类重复,手动检查目录内文件。 - 清理类加载缓存:执行以下命令清除Laravel缓存的类加载信息:
php artisan clear-compiled php artisan optimize:clear - 扩大搜索范围排查类声明:使用忽略大小写的搜索命令,覆盖更多可能的声明场景:
findstr /s /i "class CreateUsersTable" *.php - 检查文件重复加载逻辑:确认
create_users_table.php是否被其他文件通过require/include重复引入,或存在自定义迁移加载规则导致文件重复加载。 - 刷新Composer自动加载:执行
composer dump-autoload刷新自动加载规则,避免因配置问题导致迁移文件被重复扫描。
内容的提问来源于stack exchange,提问作者Joshua
相关产品推荐
相关产品推荐

