Home  >  Article  >  PHP Framework  >  laravel queries different databases

laravel queries different databases

PHPz
PHPzOriginal
2023-04-14 19:01:16127browse

With the continuous development of web applications, the amount of data is getting larger and larger, and a single database may not be able to meet the demand. In this case, multiple databases need to be used to store data. This article will introduce how to use Laravel to query different databases.

Preparation

Before you start using Laravel to query different databases, you need to complete the following preparations:

  1. Make sure Laravel is installed and running successfully. You can create it first A new project.
  2. Prepare two databases and connect them to Laravel. Add a second database connection in the .env file:

    <code>DB_CONNECTION=mysql
    DB_HOST=127.0.0.1
    DB_PORT=3306
    DB_DATABASE=database1
    DB_USERNAME=root
    DB_PASSWORD=
    
    DB_CONNECTION_SECOND=mysql
    DB_HOST_SECOND=127.0.0.1
    DB_PORT_SECOND=3306
    DB_DATABASE_SECOND=database2
    DB_USERNAME_SECOND=root
    DB_PASSWORD_SECOND=</code>

    Among them, DB_CONNECTION represents the database connection type, DB_HOST represents the database host address , DB_PORT represents the database port, DB_DATABASE represents the database name to be connected, DB_USERNAME represents the database user name, DB_PASSWORD represents the database password. DB_CONNECTION_SECOND and the following configuration represent the configuration of the second database.

  3. Create two corresponding databases and ensure that they can connect normally.

Implementation method

After completing the above preparations, we can start using Laravel to query different databases. The implementation method is as follows:

  1. First, you need to use the DB::connection() method to specify the database connection to be queried. As shown below:

    <code>use Illuminate\Support\Facades\DB;
    
    $user = DB::connection('database2')->table('users')->where('name', 'John')->first();</code>

    The above DB::connection('database2') indicates that the database named database2 is to be queried. Among them, table('users') means that the users table is to be queried.

  2. If you need to switch between different databases, you only need to call the DB::connection() method multiple times. As shown below:

    <code>$user1 = DB::connection('database1')->table('users')->where('name', 'John')->first();
    $user2 = DB::connection('database2')->table('users')->where('name', 'John')->first();</code>

    Two different databases are queried in the above code.

Conclusion

In summary, this article introduces how to use Laravel to query different databases. Just add the corresponding configuration in the .env file, and then use the DB::connection() method to specify the database connection to be queried. This is very useful for web applications that need to store large amounts of data.

The above is the detailed content of laravel queries different databases. 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