Where to put migration within a Laravel 5.1 package?

家住魔仙堡 提交于 2019-12-12 11:03:28

问题


Well, I do have a package take I only use alongside with my system. I do have migrations for that package (it was build on Laravel 4.2, and I'm upgrading it).

That being said: On my package (former workbench) on Laravel 5.1, where do I put and how do I run my migrations?

Does any of you guys know how to deal with this?

UPDATE:

This is not the case of a simple migration. Back on laravel 4.*, we were able to maintain migrations for each package (if it was so desirable), and I do have some migrations been held by my own package, in it's own database, with it's own table. So... I need it to be a PACKAGE's migrations and not a ROOT INSTALATION's migration.


回答1:


You can put it in packages/.../src/migrations. To run it:

  1. You can insert in composer.json :

"autoload": { "classmap": [ "database", "packages/.../src/migrations" ],

  1. Or just call :

    • Laravel 4.x php artisan migrate --package="{vendor}/{name}"
    • Laravel 5.x php artisan migrate --path=/packages/.../migrations

For more info : check this blog from websanova.com




回答2:


Updated Answer for Laravel 5.6+

https://laravel.com/docs/5.6/packages#migrations says:

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.

The forward-slash is important. In my case, I used: $this->loadMigrationsFrom(__DIR__ . "/migrations");.




回答3:


To create a migration:

  1. Go to command prompt locate your project root folder and run this command: php artisan make:migration yourMigrationFileName --create=tableName
  2. You can found your migration file in yourProjectFolder\database\migrations\timestamp_yourMigrationFileName.php
  3. Create your table.

To run the migration:

  1. Go to command prompt locate your project root folder and run this command: php artisan migrate

And don't forget to declare your table name in your Model. You can do it by writing protected $table = 'tableName' in your model.



来源:https://stackoverflow.com/questions/33873506/where-to-put-migration-within-a-laravel-5-1-package

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!