Multiple schemas in Laravel 5 database

Asked

Viewed 1,843 times

7

Good afternoon guys, I am redoing a system and adopted the Laravel 5. In this system I have several schemas in the database and the solution I found was to work with a connection for each schema, as follows:

'pgsql' => [
            'driver'   => 'pgsql',
            'host'     => env('DB_HOST', 'localhost'),
            'database' => env('DB_DATABASE', 'forge'),
            'username' => env('DB_USERNAME', 'forge'),
            'password' => env('DB_PASSWORD', ''),
            'charset'  => 'utf8',
            'prefix'   => '',
            'schema'   => 'schema1',
        ],

        'pgsql2' => [
            'driver'   => 'pgsql',
            'host'     => env('DB_HOST', 'localhost'),
            'database' => env('DB_DATABASE', 'forge'),
            'username' => env('DB_USERNAME', 'forge'),
            'password' => env('DB_PASSWORD', ''),
            'charset'  => 'utf8',
            'prefix'   => '',
            'schema'   => 'schema2',
        ],

Would there be any other way to do it ?

1 answer

7

The best way to work with multiple banks in the Laravel is this way:

In the configuration file you can define all the drivers to be used and the banks:

return array(

'default' => 'mysql',

'connections' => array(

    # Our primary database connection
    'mysql' => array(
        'driver'    => 'mysql',
        'host'      => 'host1',
        'database'  => 'database1',
        'username'  => 'user1',
        'password'  => 'pass1'
        'charset'   => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix'    => '',
    ),

    # Our secondary database connection
    'mysql2' => array(
        'driver'    => 'mysql',
        'host'      => 'host2',
        'database'  => 'database2',
        'username'  => 'user2',
        'password'  => 'pass2'
        'charset'   => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix'    => '',
    ),
  ),
);

After that it will be necessary to define in your models which connection to be used, as for example in the use of Eloquent it will be possible to configure only defining in the scope of the class:

protected $connection = 'mysql2';

This way all queries/changes will be made through the settings mysql2.

It is also possible to define the connection outside the class, and yes within the scope of the function, ie when you want to run X function in Y database:

public function method()
{
    $this->setConnection('mysql2');

    $this = $someModel->find(1);

    return $something;
}

It is also possible to work outside the eloquent in this way:

$users = DB::connection('mysql2')->select(...);

So your system will be ready to receive mult-connections. Show, né?

Hugs!

Sources: http://laravel.com/api/4.1/Illuminate/Database/Eloquent/Model.html

  • Is there a way to dynamically create drivers and banks? For example, the user registers into the system and a schema is created just for him. And so on, without the developer or the person responsible worrying about adding manually. + 1

  • 1

    Oops! Come on... To create the Databases you will need the Migrations: http://laravel.com/docs/5.1/migrations. where to create you can call the command by a controller http://laravel-tricks.com/tricks/run-artisan-commands-form-route-or-controller. After you create the database you will be able to store the user and his database name in the main database, when he accesses route to it http://laravel.com/docs/5.1/routing#route-group-sub-Domain-routing you configure his database. Every time you do model operation you can get the db of it on Session.

  • 1

    Example for you here: https://laracasts.com/discuss/channels/laravel/l5-multiple-databases-based-on-subdomein

Browser other questions tagged

You are not signed in. Login or sign up in order to post.