Laravel : generate migrations from 2 MySQL databases

Laravel : generate migrations from 2 MySQL databases



I've divided my database into 2 databases on MySQL and I'd like to generate migrations from both databases on laravel.
I made all the configurations but when I execute the command "php artisan migrate:generate" it creates migrations only for the first database.
Does anyone know how to create the migrations of the 2 databases ?



Thanks for your help :)



My config/database.php file


'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => false,
'engine' => 'InnoDB ROW_FORMAT=DYNAMIC' ,
],

'mysql2' => [
'driver' => 'mysql',
'host' => env('DB_HOST_2', '127.0.0.1'),
'port' => env('DB_PORT_2', '3306'),
'database' => env('DB_DATABASE_2', 'forge1'),
'username' => env('DB_USERNAME_2', 'forge1'),
'password' => env('DB_PASSWORD_2', ''),
'unix_socket' => env('DB_SOCKET_2', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => false,
'engine' => 'InnoDB ROW_FORMAT=DYNAMIC' ,
],



My .env file


DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=evamesure
DB_USERNAME=root
DB_PASSWORD=

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=evamesure1
DB_USERNAME=root
DB_PASSWORD=





I think that can generate conflicts is better if you want to change database better modify the .env
– Alexander Villalobos
Aug 31 at 13:02


database


.env




3 Answers
3



Run migrate:generate, then update your DB_CONNECTION env variable to mysql2 and run it again.


migrate:generate


DB_CONNECTION


mysql2



Do not put duplicate keys in your .env file. In your config file you suffixed _2 to the second connection's env variables but you aren't using that suffix in .env.


_2



If you want all environments to use two databases, you'll have to manually insert the connection name in the 2nd database's migrations and models.





Thanks I have created migrations for both databases. This means that in my .env and database.php file I keep the connection only from one database ?
– Patrick_jr
Aug 31 at 13:45





yeah if you want it all in one db going forward
– Devon
Aug 31 at 13:55



if you are looking to migrate 2 database what you can do is this in your .env
run first migration with php artisan migrate


.env


php artisan migrate


DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=evamesure
DB_USERNAME=root
DB_PASSWORD=



later when complete first migration update your .env to next database and run again php artisan migrate


migration


.env


database


php artisan migrate


DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=evamesure1
DB_USERNAME=root
DB_PASSWORD=



I don't know about generating the migrations, but when you're writing your migrations, you are able to specify a connection that you want to run a migration on:


Schema::connection('mysql')->create('your_table', function (Blueprint $table)
// This table will only be created for the mysql connection
);

Schema::connection('mysql2')->create('your_table', function (Blueprint $table)
// This table will only be created for the mysql2 connection
);



That will allow you to have different tables for different databases. If you want to have the same tables in both databases, you could take the same approach:


foreach (['mysql', 'mysql2'] as $connection)
Schema::connection($connection)->create('your_table', function (Blueprint $table)
// Create a table for each database
);



Don't forget that you will need to specify the relevant connection in your models if you want a certain model to use a non-default connection:


class User extends Model

protected $connection = 'mysql2';



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Some of your past answers have not been well-received, and you're in danger of being blocked from answering.



Please pay close attention to the following guidance:



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)