For developers working on projects and feel the need to separate their tables into different categories Laravel framework makes it manageable even when switching Database Connections and SQL drivers (MYSQL || PGSQL || SQLITE || SQLSRV)
Working on a project and wanna configure Images table to be on a different MYSQL beyond your base database where your Users table exists
OR
Products table to be handled on PGSQL far away from your MySQL Users table
OR
Your tables handled by different third party API's and change DB_HOST connection
...
LIST IS ENDLESS WHY CHOOSING THIS OPTION!
- Folder: config > File: database.php > Set your Database Connections and Connection names to Configure on
Schema::connection('')
on Folder: database. - Folder: database > php artisan make:migration to create table then add
Schema::connection('')
to the Connection name where you want the table exist. - Models Folder: Add Database Connection 'connection' to your Models eg
protected $connection = 'asset'
on Image.php Model (extension of Illuminate\Database\Eloquent\Model.php) - DONE!
-
Laravel Queue:
-
Add
'database' => ['connection' => 'queue']
to'connections'
(If you have created a QUEUE connection and CHANGED'default' => env('QUEUE_DRIVER', 'database')
). -
Change
'failed' => ['database' => 'queue']
-
Laravel Session:
-
Change
'driver' => env('SESSION_DRIVER', 'database')
-
Change
'connection' => null
to Connection name you want table where database exists ie'connection' => 'activity'
- Whoever knows how to change Laravel Notification database connection from base table
'mysql'
, Please contribute without affecting CORE LARAVEL LIBRARY (Illuminate\Notifications\DatabaseNotifications.php)
- This Configuration affects Model and Table Relations.
- Make sure Migration foreign references database + table eg
foreign('user_id')->references('id')->on('*_core.users')->onDelete('cascade')
- Make sure to add Connection name on Model User ie
protected $connection = 'mysql'
. - Make sure on Request
rules()
to add Connection name + table eg'unique:mysql.users'
(eg If you see exception'*_asset.users table does not exist'
)
Feel free to Contribute more If you working on such environments and want to contribute on dividing tables on deeper but easily manageable steps.
If you discover a security vulnerability within Laravel or such conditions, please send an e-mail to Laravel team [email protected].
The Laravel framework is open-sourced software licensed under the MIT license.