ThinkPHP 8.0 迁移文件必须置于 database/migrations/ 目录,命名空间为 DatabaseMigrations,类名与文件名一致,并继承 thinkmigrationMigration;需配置 composer.json 的 PSR-4 自动加载并执行 composer dump-autoload 刷新映射。

因为 ThinkPHP 8.0 不扫描 app/database 目录,迁移文件必须放在 database/migrations(标准 Laravel 风格路径),且类命名空间需为 Database\Migrations,否则自动加载失败。
迁移文件位置和命名空间必须匹配
TP8.0 的迁移命令 php think migrate:run 默认只读取 database/migrations/ 下的 PHP 文件,并要求每个迁移类的命名空间是 DatabaseMigrations。把文件放进 app/database/ 是无效的——这个目录不在 PSR-4 自动加载规则中,框架根本不会尝试加载它里面的类。
-
正确路径:新建
database/migrations/2026_10_20_000000_create_users_table.php -
正确命名空间:文件首行必须是
namespace DatabaseMigrations; -
类名须与文件名一致:如
class CreateUsersTable extends Migration
自动加载未生效或类映射缺失
即使路径和命名空间都对了,如果 Composer 类映射没更新,依然会报 Class not found。TP8.0 完全依赖 Composer 的 PSR-4 加载机制,不走旧版的路径扫描。
- 执行
composer dump-autoload或php think optimize:autoload刷新映射 - 检查
composer.json中是否已配置:"autoload": { "psr-4": { "Database\Migrations\": "database/migrations/" } } - 若手动改过 autoload 配置,务必运行
composer install或composer update生效
迁移类继承和方法签名要合规
TP8.0 的迁移基类已变更,不再兼容 TP5/6 的写法。必须继承 thinkmigrationMigration,且 up() / down() 方法参数类型需严格匹配。
- 错误写法:
public function up($schema)(缺少类型提示) - 正确写法:
public function up(Schema $schema): void - 确保已安装
topthink/think-migration扩展,且版本适配 TP8.0(≥3.0)


















