Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where to put migration within a Laravel 5.1 package?

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.

like image 283
Dennis Braga Avatar asked Sep 27 '22 03:09

Dennis Braga


2 Answers

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

like image 185
4givN Avatar answered Oct 11 '22 04:10

4givN


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");.

like image 23
Ryan Avatar answered Oct 11 '22 05:10

Ryan