You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.25 08:13:24