问题
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:
- You can insert in composer.json :
"autoload": {
"classmap": [
"database",
"packages/.../src/migrations"
],
Or just call :
- Laravel 4.x
php artisan migrate --package="{vendor}/{name}"
- Laravel 5.x
php artisan migrate --path=/packages/.../migrations
- Laravel 4.x
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
:
- Go to
command prompt
locate your project root folder and run this command:php artisan make:migration yourMigrationFileName --create=tableName
- You can found your migration file in
yourProjectFolder\database\migrations\timestamp_yourMigrationFileName.php
- Create your table.
To run the migration
:
- 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