Detailed Explanation of Multi database Connection Operation Based on Laravel Framework

  • 2021-12-13 07:40:49
  • OfStack

In this paper, an example is given to describe the implementation of multi-database connection operation by Laravel framework. Share it for your reference, as follows:

This article introduces the method of connecting two databases in laravel

1. Define a connection

In the database configuration file app/config/database. php, you can define multiple database connections that are identical or different. For example, if you want to grab data from 2 MYSQL data into your program, you can define it as follows:


<?php
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'  => '',
    ),
  ),
);

The default connection is still mysql, unless you specify another connection, such as mysql2, our connections are all mysql connections.

2. Specify the connection

Now let's specify the mysql2 connection. What should we do:

Schema Database Migration

You can create any connection with Schema facade. Now you just need to use connection() Method to create an table in the specified database


Schema::connection('mysql2')->create('some_table', function($table)
{
  $table->increments('id'):
});

If not connection() Method is to create table in the default database

Query

Like the above 1, use connection() Method


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

Eloquent

Specify the connection database method in the model and set the $connection variable in the model


<?php
class SomeModel extends Eloquent {
  protected $connection = 'mysql2';
}

The specified database can also be connected by setConnection method in the controller


<?php
class SomeController extends BaseController {
  public function someMethod()
  {
    $someModel = new SomeModel;
    $someModel->setConnection('mysql2');
    $something = $someModel->find(1);
    return $something;
  }
}

Cross-database connectivity is OK, but it can cause some problems, depending on your database or database configuration, so use it with caution.

Original address: http://fideloper.com/laravel-multiple-database-connections

More readers interested in Laravel can check the topics of this site: "Introduction and Advanced Tutorial of Laravel Framework", "Summary of Excellent Development Framework of php", "Introduction Tutorial of php Object-Oriented Programming", "Introduction Tutorial of php+mysql Database Operation" and "Summary of Common Database Operation Skills of php"

I hope this article is helpful to the PHP programming based on Laravel framework.


Related articles: