Home >PHP Framework >Laravel >How to connect two databases to query data in laravel

How to connect two databases to query data in laravel

PHPz
PHPzOriginal
2023-04-12 09:13:391338browse

With the continuous advancement and development of network technology, modern Web applications have become an indispensable part today. However, in Web applications, data management is obviously also a crucial link. Furthermore, for large web applications, there are usually multiple databases. For example, an e-commerce platform, in addition to the basic product information database, will also have different databases such as user information, order information, payment information, etc. So, how to connect multiple databases and query data under the Laravel framework? This article will provide a method that can be followed.

First, you need to configure multiple databases in Laravel's database configuration file config/database.php, as shown below:

'connections' => [

    'mysql' => [
        //mysql主数据库
        'driver' => 'mysql',
        'host' => 'localhost',
        'database' => 'db1',
        'username' => 'root',
        'password' => '',
    ],

    'mysql2' => [
        //mysql2次数据库
        'driver' => 'mysql',
        'host' => 'localhost',
        'database' => 'db2',
        'username' => 'root',
        'password' => '',
    ],

],

Two database connections, mysql and mysql2, are defined in the above configuration file. . The specific configuration can be adjusted according to your own needs.

Next, two database connections need to be defined. You can create a new base class ModelBase in the /model directory and define multiple connections in it.

<?php
namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class ModelBase extends Model
{
    // mysql
    protected $connection = &#39;mysql&#39;;

    // mysql2
    protected $connection2 = &#39;mysql2&#39;;

    protected function getConnectionName()
    {
        if ($this->getConnection() === $this->connection2) {
            return $this->connection2;
        }
        return $this->connection;
    }

    public function getTable()
    {
        $table = parent::getTable();

        if ($this->getConnection() === $this->connection2) {
            $table = 'db2.' . $table;
        }
        return $table;
    }

}

The above code defines two connections: mysql and mysql2. And, define the two functions getConnectionName and getTable. The getConnectionName function returns the current database connection name, and the getTable function is used to obtain the current database table.

Finally, use in the actual Model:

namespace App\Models;
class UserModel extends ModelBase
{
    protected $table = 'user';
}

In the Model, inherit from ModelBase, and when defining $table, you only need to write the table name.

The above is a method to connect two databases to query data in Laravel. In this way, query operations of multiple databases can be realized. For large-scale applications, this method can effectively solve query problems between multiple databases, making the program run more efficiently and stably.

The above is the detailed content of How to connect two databases to query data in laravel. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn