Use one Laravel migrations table per database
Article on usage of multiple database usage in Laravel -
https://stackcoder.in/posts/laravel-7x-multiple-database-connections-migrations-relationships-querying
Use the --database
parameter with the migrate
command and store the migrations for each database in separate directories.
You could have separate directories in app/database/migrations
for each of your database (in your case db1
and db2
) and store the appropriate migrations in each directory. Then you could run the migrations like this:
artisan migrate --database="db1" --path="app/database/migrations/db1"
artisan migrate --database="db2" --path="app/database/migrations/db2"
This way your migrations
table will be independent for each database.
If you want to go the extra mile and automate the process you could create your custom command that will run all the migrations at once. You can create the command like this (use make:console
for Laravel 5.0 up to 5.2 or make:command
for Laravel 5.2+):
artisan command:make MigrateAllCommand --command=migrate:all
This will create a new file app/commands/MigrateAllCommand.php
. Your command's fire
method would look something like this:
public function fire()
{
foreach (Config::get('database.connections') as $name => $details)
{
$this->info('Running migration for "' . $name . '"');
$this->call('migrate', array('--database' => $name, '--path' => 'app/database/migrations/' . $name));
}
}
This will work provided the name of the database configuration key is the same as the migration directory name. You can then just call it like this:
artisan migrate:all
You can check the Laravel Command Docs for more info.
Can i migrate one table without affecting my data in the database?
When you run php artisan migrate
it would run any migrations that haven't yet been run and it wouldn't remove your existing data unless you're deleting columns. You could also run a specific single migration by adding the path to the file with the --path
parameter, e.g. php artisan migrate --path=database/migrations/2019_04_10_migration_name.php
.
Laravel multiple tables per migration
During development of your application I don't think you should care too much having only one table per migration, sometimes it's just easier to have some tables togheter in a single migration, but as soon as your system go to production, you will not be able to keep working like that, because you will only migrate in production and probably never rollback, so your migrations will be really small, sometimes you'll have a migration for a single column creation.
The advantages of putting tables in different migrations is the same of having a thin class, the less information you have in one file, the easier is to manage and make changes on it. So if you put all tables in a single migration, it gets harder to maintain, but that's really up to you.
Foreign keys are a good example of why you should create one migration per table and even per foreign key: every time you rollback a table related to foreign keys, you must first delete all foreign dependencies, that's why Laravel creates a migrates them all in the same order, it helps you never screw a table drop. So, create your tables migrations first, then you create your foreign keys migrations, so when you rollback it will first rollback the constraints and then the tables.
I create foreign keys for a table in the same migration of that table, unless I have too much cross foreign keys. But I always create a foreign key in a separate Schema::table()
command, because some databases need you to have the column before attaching the constraint to it:
public function up()
{
Schema::create('statuses', function(Blueprint $table)
{
$table->string('id', 64)->primary();
$table->string('user_id', 64)->index();
$table->text('body');
$table->timestamps();
});
Schema::table('statuses', function(Blueprint $table)
{
$table->foreign('user_id')
->references('id')
->on('users')
->onUpdate('cascade')
->onDelete('cascade');
});
}
About many to many, if you create the table and foreign keys togheter, you should first create the master and then the pivot tables, but if you are creating your foreign keys in separate migrations, first create the tables (order will not matter much, but it's also better to be organized in those cases) and then the migrations for the foreign keys.
During development I do a lot of changes in my tables, so I'm always coming back to them, so this is what I use to do, when I'm altering a migration:
1) php artisan migrate:reset
many times
2) Alter migration
3) php artisan migrate
If I'm just creating a new one, usually I won't have any problems, because migrations are usually idepotent.
Your last question was already answered, but I'll say it again: Laravel name the migrations files using timestamps, the way you will never have a migration being ran before another one created before it:
2014_07_16_190821_create_statuses_table
And the name of the migration matter, because this one above will create this class:
CreateStatusesTable
So one thing you must do is to create every migration with a different name, otherwise you will end up with two classes with the same name and, not Laravel, but PHP will complaint about it.
laravel multiple databases with multiple migration tables
I found it out with the help of this Use one Laravel migrations table per database and post the answer here for others.
In order to separate the migrations you need to run the migrate command with the database option like this:
php artisan migrate --database="nameOfConnection"
Related Topics
Data from Two Tables with Same Column Names
PHP Linefeeds (\N) Not Working
Laravel Compact() and ->With()
PHP Get Request, Sending Headers
Foursquare API for Venue User Image Error
Loadhtml Libxml_Html_Noimplied on an HTML Fragment Generates Incorrect Tags
Using Mx Records to Validate Email Addresses
Strcmp Equivelant for Integers (Intcmp) in PHP
Reflectionexception: Class Classname Does Not Exist - Laravel
PHP Strtotime "Last Monday" If Today Is Monday
Change Foreign Characters to Their Roman Equivalent
How Effective Is the Honeypot Technique Against Spam
Reverse Geocoding with Google Map API and PHP to Get Nearest Location Using Lat,Long Coordinates
How to Manage a Single PHP5 Session on Multiple Apache Servers
How to Compare 2 HTML Pages, and Output Only the Different Bits in Ruby or PHP