How to connect multiple database in laravel
You can use multiple databases in several ways. "DB" facade or Eloquent, both supports using multiple database connections.
Laravel has a built-in Database Manager that allows you to create multiple database connections. You can define different databases in your config/database.php file, and then specify which database to use in your models or queries.
For example, you can define two databases like this in your config/database.php file:
You can also use the Query Builder to perform database operations on multiple databases. In this case, you will need to specify the database name in the query.
The Eloquent ORM in Laravel also supports multiple database connections. You can define the database connection to use for a particular model by specifying the connection property in the model.
Once you have set the connection property, you can use Eloquent methods as usual:
These are some of the ways you can use multiple databases in Laravel. Choose the method that works best for your application and follow the Laravel documentation for more details.
Database Connections
Laravel has a built-in Database Manager that allows you to create multiple database connections. You can define different databases in your config/database.php file, and then specify which database to use in your models or queries.For example, you can define two databases like this in your config/database.php file:
'default' => [ 'driver' => 'mysql', 'host' => env('DB_HOST', '127.0.0.1'), 'database' => env('DB_DATABASE', 'database1'), 'username' => env('DB_USERNAME', 'root'), 'password' => env('DB_PASSWORD', ''), 'charset' => 'utf8mb4', 'collation' => 'utf8mb4_unicode_ci', 'prefix' => '', 'strict' => true, 'engine' => null, ], 'secondary' => [ 'driver' => 'mysql', 'host' => env('SECONDARY_DB_HOST', '127.0.0.1'), 'database' => env('SECONDARY_DB_DATABASE', 'database2'), 'username' => env('SECONDARY_DB_USERNAME', 'root'), 'password' => env('SECONDARY_DB_PASSWORD', ''), 'charset' => 'utf8mb4', 'collation' => 'utf8mb4_unicode_ci', 'prefix' => '', 'strict' => true, 'engine' => null, ],In this example, we have defined two database connections, default and secondary. You can use these connections in your models or queries like this:
$users = DB::connection('secondary')->table('users')->get();
Query Builder
You can also use the Query Builder to perform database operations on multiple databases. In this case, you will need to specify the database name in the query.
$users = DB::table('database1.users') ->join('database2.contacts', 'users.id', '=', 'contacts.user_id') ->select('users.*', 'contacts.phone') ->get();In this example, we are selecting data from two databases, database1 and database2 , by prefixing the table names with the database name.
Eloquent ORM
The Eloquent ORM in Laravel also supports multiple database connections. You can define the database connection to use for a particular model by specifying the connection property in the model.
class User extends Model { protected $connection = 'secondary'; }In this example, we are telling Laravel to use the secondary database connection for the User model.
Once you have set the connection property, you can use Eloquent methods as usual:
$users = User::where('age', '>', 18)->get();This will fetch all users who are over 18 years old from the secondary database.
These are some of the ways you can use multiple databases in Laravel. Choose the method that works best for your application and follow the Laravel documentation for more details.
Comments (0)