laravel-multidb
laravel-multidb copied to clipboard
Laravel model relationship with multiple database connections
Laravel Multiple Database Connection
This is an example of Laravel with multiple database connections for operating Eloquent Relationship and join query.
There are three connections:
mysql(MySQL) as defaultmysql2(MySQL)sqlsrv(SQL Server 2016)
Installing & Running
git clone https://github.com/didikz/laravel-multidb.git
composer install
Copy .env.example as .env file and setup your database configuration. If you don't have SQL server, feel free to use another database engine but don't forget to adjust your new connection.
php artisan key:generate
php artisan migrate
php artisan db:seed --class=IsiTableSeeder
php artisan serve
Visit your browser to http://localhost:8000/publishing to get data from multiple database connections. Open routes/web.php to check available routes;
Table Migration & Model
There are three migration tables with different connection:
typestable / model inmysql2connectionpoststable / model inmysqlconnectionmailstable / model insqlsrvconnection
Edit your migration & model connection if you used different connection.