Laravel 10 Multiple Database Connection Example

Laravel 10 Multiple Database Connection Example

Laravel 10 is a popular PHP framework that makes it easy to develop web applications quickly and efficiently. One of the many features of Laravel 10 is its ability to work with multiple databases. In this article, we will go over an example of how to set up and use multiple database connections in Laravel 10.

Setting up Multiple Database Connections in Laravel 10

To set up multiple database connections in Laravel 10, we first need to modify our database configuration file, which can be found at config/database.php. In this file, we need to add our additional database connections under the connections array. Here’s an example of how to add a second database connection:

'connections' => [

    'mysql' => [
        // default database connection
    ],

    'second_database' => [
        'driver' => 'mysql',
        'host' => 'localhost',
        'port' => '3306',
        'database' => 'second_database',
        'username' => 'username',
        'password' => 'password',
        'charset' => 'utf8mb4',
        'collation' => 'utf8mb4_unicode_ci',
        'prefix' => '',
        'strict' => true,
        'engine' => null,
    ],

],

This code defines two database connections in Laravel 8, mysql and second_database. The mysql connection is the default database connection and doesn’t have any configuration specified in this example.

The second_database connection is defined using the mysql driver and has several configuration options specified:

  • host: This is the hostname or IP address of the server where the database is located.
  • port: This is the port number used to connect to the database server. The default port for MySQL is 3306.
  • database: This is the name of the database that we want to connect to.
  • username: This is the username used to authenticate with the database server.
  • password: This is the password used to authenticate with the database server.
  • charset: This is the character set used for database connections. The default character set for MySQL is utf8mb4.
  • collation: This is the collation used for database connections. The default collation for MySQL is utf8mb4_unicode_ci.
  • prefix: This is the table prefix used for the database connection.
  • strict: This option specifies whether or not to enable strict mode for the database connection. When strict mode is enabled, the database server will return an error if invalid data is inserted into a table.
  • engine: This option specifies the default storage engine to use for the database connection. The default storage engine for MySQL is InnoDB.

By defining multiple database connections in Laravel 8, we can work with multiple databases in our application, each with their own unique configuration options. We can switch between connections by specifying the connection name in our Eloquent models or database queries.

As you can see, we’ve added a new connection called second_database. We’ve specified the database driver, host, port, database name, username, and password for this connection. We’ve also set the character set, collation, prefix, strict mode, and engine for this connection.

Using Multiple Database Connections in Laravel 10

Now that we have set up our additional database connection, we can use it in our application. To use a specific database connection, we need to specify its name in our Eloquent models or in our database queries.

To specify a database connection in our Eloquent model, we can define a connection property that points to the name of the database connection we want to use. For example:

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class User extends Model
{
    /**
     * The connection name for the model.
     *
     * @var string
     */
    protected $connection = 'second_database';

    // ...
}

This is a PHP code snippet that defines a model class User in the Laravel framework. The model extends the Model class provided by Laravel’s Eloquent ORM (Object-Relational Mapping) system.

The namespace statement declares the namespace in which the User class is defined. The use statement imports the Model class from the Illuminate\Database\Eloquent namespace, which is used as the base class for the User model.

The User class has a $connection property that specifies the name of the database connection to be used by the model. In this case, it is set to second_database, which means that the User model will use a database connection with the same name.

The User class can then be used to perform CRUD (Create, Read, Update, Delete) operations on the corresponding database table using the Eloquent ORM.

In this example, we’ve defined a User model that uses the second_database connection.

We can also specify the database connection when querying the database using the connection method. For example:

DB::connection('second_database')->table('users')->where('name', 'John')->get();

In this example, we’re using the DB facade to query the users table in the second_database connection.

Conclusion

In this article, we’ve gone over an example of how to set up and use multiple database connections in Laravel 10. By modifying the database configuration file and specifying the database connection in our Eloquent models or database queries, we can easily work with multiple databases in our Laravel 10 application.

This Article Ideas has been taken from the following websites; which are following:

https://fideloper.com/laravel-multiple-database-connections

https://techvblogs.com/blog/laravel-10-multiple-database-connections-example

Laravel Multiple Database Connection Example

https://www.itsolutionstuff.com/post/laravel-10-multiple-database-connections-exampleexample.html

https://laravel.com/docs/10.x/database

https://codelapan.com/post/how-to-use-multiple-database-connections-in-laravel-8

https://techsolutionstuff.com/post/how-to-connect-multiple-database-in-laravel-9

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *