K
K
Konstantin Andreevich2014-09-01 10:40:23
MySQL
Konstantin Andreevich, 2014-09-01 10:40:23

Laravel: work with a large number of different databases on remote servers. How?

A small preface - there are many sites-services, almost identical, with a single database structure. The whole thing is written in Yii 1.x, now we are rewriting it in Laravel, and we will use only Laravel - such conditions. Each such site has its own admin panel, through which information is edited. This is tolerable when there are less than 10 sites, but this figure has long surpassed 100.
So. Now the question is how to organize a single admin panel for all sites. The meaning is very simple - we go to the admin panel and see a list of all sites (we add them there beforehand, indicating the MySQL server). By clicking on any site, we are prompted to enter a login and password from the database (which, of course, is not saved). After that, we have the usual admin panel.
Now the question is how to implement this in Laravel? How to dynamically create remote connections from code and work with them? Advise something, please.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
K
Konstantin Andreevich, 2014-09-01
@reffy

Everything turned out to be quite simple.
First, create a new config:

$conn = array(
    'driver'    => 'mysql',
    'host'      => 'localhost',
    'database'  => 'DATABASE',
    'username'  => 'USERNAME',
    'password'  => 'SOME_PASSWORD',
    'charset'   => 'utf8',
    'collation' => 'utf8_general_ci',
    'prefix'    => '',
);

Config::set('database.connections.DB_CONFIG_NAME', $conn);

Then we can work with the remote database:
MODEL::on('DB_CONFIG_NAME')->find(1);

// чтобы не указывать постоянно on('DB_CONFIG_NAME')
// мы можем указать через какой конфиг подключаемся
// в самой моделе
class Page extends Eloquent
{
  // через переменную $connection
  protected $connection = 'DB_CONFIG_NAME';

  // либо в конструкторе
  function __construct()
  {
    $this->connection = 'DB_CONFIG_NAME';
  }

S
Sergey, 2014-09-01
Protko @Fesor

smoke in the direction of the local IoC, which resolves the parameters for connections. You can add a parameter to the session that determines which database we want to work with, and catch this case before processing the request by switching databases.
It all depends on the architecture of the application, but the essence is about the same.
Another option is to register all connection options in the config and select the one you need through the same IoC and set it as the default.

S
Sergey Romanov, 2014-09-01
@Serhioromano

As I understand it, you will add sites. So they will be stored somewhere in the database. In the same place, save the data for the connection. And when the site is selected just initialize this connection.
Or you can create all connections in the connection configuration file, give each one its own name and then use this name to work with the database via DB::connection($name)->...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question