如何为 laravel 迁移指定文件夹?

How to specify folder for laravel migrations?

我正在尝试将 laravel 数据库迁移放在子文件夹中,但不确定如何执行此操作。

您可能想看看这个包,它不是您要找的东西,但它是排序和组织应用程序部分(包括控制器、迁移、模型、视图等)的替代方法。

Nwidart's Laravel Modules 用于 Laravel、

的模块化应用程序开发

它有助于将您的应用程序组织成模块,因此,您可以在每个模块中创建迁移,并且由于模块位于单独的文件夹中,因此它有助于以某种方式解决此问题。

创建新迁移和执行迁移时,您可以通过命令行界面传入 path 参数,以分别指定它将用于创建和 运行 迁移的目录。

php artisan make:migration create_users_table --path=/path/to/your/migration/directory

php artisan migrate --path=/path/to/your/migration/directory

如果您急于这样做,nscreed/laravel-migration-paths 包中有一个快速解决方案。非常简单易用。

During the periodical development phase the migrations folder may become very large. It is very helpful if we can organize the content of the migration folders. This library helps to organize migration files in different folders. Even, if your organize your existing files it will works as well.

希望对你有所帮助

AppServiceProvider中找到一个boot方法并添加如下

$mainPath = database_path('migrations');
$directories = glob($mainPath . '/*' , GLOB_ONLYDIR);
$paths = array_merge([$mainPath], $directories);
         
$this->loadMigrationsFrom($paths);
         

现在您可以使用 php artisan migrate,在子文件夹中进行迁移时也可以使用 php artisan migrate:back