Question:

How can I use multiple database connections in Laravel?

Answer:

In Laravel, you can configure multiple database connections easily by specifying them in the `config/database.php` file. Simply add another array under the `connections` key, providing the necessary database details for the additional connection. After configuring the connections, you can use the `DB` facade to specify the desired connection using the `connection` method. For example:

```php

$users = DB::connection('mysql')->select('select * from users');

$orders = DB::connection('mysql2')->select('select * from orders');

```

Here, `mysql` and `mysql2` are the names of the connections defined in the configuration file.

#laravel

Reply to this note

Please Login to reply.

Discussion

No replies yet.