将迁移放在 Laravel 5.1 包中的什么位置?
Where to put migration within a Laravel 5.1 package?
好吧,我确实有一个软件包,我只将它与我的系统一起使用。我确实有那个包的迁移(它是在 Laravel 4.2 上构建的,我正在升级它)。
话虽这么说:在我的包裹上(以前的workbench)在Laravel 5.1上,我应该放在哪里以及如何放置运行 我的迁移?
有大佬知道怎么处理吗?
更新:
这不是简单的迁移。回到 laravel 4.*,我们能够为每个包维护迁移(如果需要的话),我确实有一些迁移由我自己的包保存,在它自己的数据库中,有它自己的 table。所以...我需要它是 PACKAGE 的迁移,而不是 ROOT INSTALATION 的迁移。
创建 migration
:
- 转到
command prompt
找到您的项目根文件夹并 运行 此命令:php artisan make:migration yourMigrationFileName --create=tableName
- 您可以在
yourProjectFolder\database\migrations\timestamp_yourMigrationFileName.php
中找到您的迁移文件
- 创建您的 table.
致运行 migration
:
- 转到
command prompt
找到您的项目根文件夹并 运行 此命令:php artisan migrate
并且不要忘记在您的 Model
中声明您的 table 姓名。您可以通过在模型中写入 protected $table = 'tableName'
来实现。
你可以把它放在packages/.../src/migrations
里。
给运行吧:
- 您可以在 composer.json 中插入:
"autoload": {
"classmap": [
"database",
"packages/.../src/migrations"
],
或直接致电:
- Laravel 4.x
php artisan migrate --package="{vendor}/{name}"
- Laravel 5.x
php artisan migrate --path=/packages/.../migrations
更多信息:查看this blog from websanova.com
Laravel 5.6+
的更新答案
https://laravel.com/docs/5.6/packages#migrations 说:
If your package contains database migrations, you may use the
loadMigrationsFrom method to inform Laravel how to load them. The
loadMigrationsFrom method accepts the path to your package's
migrations as its only argument:
/**
* Perform post-registration booting of services.
*
* @return void
*/
public function boot()
{
$this->loadMigrationsFrom(__DIR__.'/path/to/migrations');
}
Once your package's migrations have been registered, they will
automatically be run when the php artisan migrate command is
executed. You do not need to export them to the application's main
database/migrations directory.
正斜杠很重要。就我而言,我使用了:$this->loadMigrationsFrom(__DIR__ . "/migrations");
.
好吧,我确实有一个软件包,我只将它与我的系统一起使用。我确实有那个包的迁移(它是在 Laravel 4.2 上构建的,我正在升级它)。
话虽这么说:在我的包裹上(以前的workbench)在Laravel 5.1上,我应该放在哪里以及如何放置运行 我的迁移?
有大佬知道怎么处理吗?
更新:
这不是简单的迁移。回到 laravel 4.*,我们能够为每个包维护迁移(如果需要的话),我确实有一些迁移由我自己的包保存,在它自己的数据库中,有它自己的 table。所以...我需要它是 PACKAGE 的迁移,而不是 ROOT INSTALATION 的迁移。
创建 migration
:
- 转到
command prompt
找到您的项目根文件夹并 运行 此命令:php artisan make:migration yourMigrationFileName --create=tableName
- 您可以在
yourProjectFolder\database\migrations\timestamp_yourMigrationFileName.php
中找到您的迁移文件
- 创建您的 table.
致运行 migration
:
- 转到
command prompt
找到您的项目根文件夹并 运行 此命令:php artisan migrate
并且不要忘记在您的 Model
中声明您的 table 姓名。您可以通过在模型中写入 protected $table = 'tableName'
来实现。
你可以把它放在packages/.../src/migrations
里。
给运行吧:
- 您可以在 composer.json 中插入:
"autoload": {
"classmap": [
"database",
"packages/.../src/migrations"
],
或直接致电:
- Laravel 4.x
php artisan migrate --package="{vendor}/{name}"
- Laravel 5.x
php artisan migrate --path=/packages/.../migrations
- Laravel 4.x
更多信息:查看this blog from websanova.com
Laravel 5.6+
的更新答案https://laravel.com/docs/5.6/packages#migrations 说:
If your package contains database migrations, you may use the loadMigrationsFrom method to inform Laravel how to load them. The loadMigrationsFrom method accepts the path to your package's migrations as its only argument:
/**
* Perform post-registration booting of services.
*
* @return void
*/
public function boot()
{
$this->loadMigrationsFrom(__DIR__.'/path/to/migrations');
}
Once your package's migrations have been registered, they will automatically be run when the php artisan migrate command is executed. You do not need to export them to the application's main database/migrations directory.
正斜杠很重要。就我而言,我使用了:$this->loadMigrationsFrom(__DIR__ . "/migrations");
.