Home >PHP Framework >Laravel >How to do database configuration in Laravel

How to do database configuration in Laravel

PHPz
PHPzOriginal
2023-04-14 16:54:142290browse

Laravel is a powerful PHP development framework that allows developers to simplify the development process and improve efficiency. Laravel is a framework based on the MVC pattern and supports a variety of database systems. When using Laravel for web development, you often need to configure the database. This article will introduce how to configure database in Laravel.

1. Open the environment configuration file

Laravel’s database connection is configured in the .env file. Open the .env file and find the DB\_CONNECTION field. Its corresponding value defaults to mysql. If you are using another database system, such as PostgreSQL or SQLite, just change this value to the corresponding database system name.

2. Configure the database name and login credentials

By default, Laravel uses the MySQL database, so we need to configure the database authentication credentials and database name. In the .env file, locate the following fields:

DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=my_database
DB_USERNAME=my_username
DB_PASSWORD=my_password
  • DB_HOST: The IP address or host name of the database server
  • DB_PORT: The port number of the database server
  • DB_DATABASE : Database name
  • DB_USERNAME: Username to log in to the database
  • DB_PASSWORD: Password to log in to the database

Just modify the values ​​of the above fields according to the actual situation of the project.

3. Change the default database

If you need to change the default database, you can modify the config/database.php file. First find the value of the DB_CONNECTIONS key-value pair, which is an array. The array contains all databases supported by Laravel, including MySQL, SQLite, PostgreSQL, etc. Find the configuration item corresponding to the database system you want to use, and modify it to your actual configuration.

4. Use multiple databases

In some cases, we need to use multiple databases, such as a master database and multiple slave databases, or different users need to connect to different database. Laravel supports multiple database connections. In the config/database.php file, we can define multiple database connections.

'connections' => [
    'mysql' => [
        'driver' => 'mysql',
        'host' => env('DB_HOST', 'localhost'),
        'port' => env('DB_PORT', '3306'),
        'database' => env('DB_DATABASE', 'forge'),
        'username' => env('DB_USERNAME', 'forge'),
        'password' => env('DB_PASSWORD', ''),
        'unix_socket' => env('DB_SOCKET', ''),
        'charset' => 'utf8mb4',
        'collation' => 'utf8mb4_unicode_ci',
        'prefix' => '',
        'prefix_indexes' => true,
        'strict' => true,
        'engine' => null,
    ],

    'mysql2' => [
        'driver' => 'mysql',
        'host' => env('DB_HOST_2', 'localhost'),
        'port' => env('DB_PORT_2', '3306'),
        'database' => env('DB_DATABASE_2', 'forge'),
        'username' => env('DB_USERNAME_2', 'forge'),
        'password' => env('DB_PASSWORD_2', ''),
        'unix_socket' => env('DB_SOCKET_2', ''),
        'charset' => 'utf8mb4',
        'collation' => 'utf8mb4_unicode_ci',
        'prefix' => '',
        'prefix_indexes' => true,
        'strict' => true,
        'engine' => null,
    ],
],

The above example defines two database connections, one is mysql and the other is mysql2. We can use these two connections in our code.

5. Use configuration files for database configuration

When using multiple database systems, we will define multiple database connection configurations in the .env file at the same time. In order to improve the readability and portability of the code, we can write the database connection configuration into a special configuration file.

Find the connections key-value pair in the config/database.php file, and then add the following code in the array:

'mysql' => [
    'driver' => 'mysql',
    'host' => env('DB_HOST', 'localhost'),
    'port' => env('DB_PORT', '3306'),
    'database' => env('DB_DATABASE', 'forge'),
    'username' => env('DB_USERNAME', 'forge'),
    'password' => env('DB_PASSWORD', ''),
    'unix_socket' => env('DB_SOCKET', ''),
    'charset' => 'utf8mb4',
    'collation' => 'utf8mb4_unicode_ci',
    'prefix' => '',
    'prefix_indexes' => true,
    'strict' => true,
    'engine' => null,
],

Next, create the database.php file in the config directory with the following content:

return [
    'mysql' => [
        'driver' => 'mysql',
        'host' => env('DB_HOST', 'localhost'),
        'port' => env('DB_PORT', '3306'),
        'database' => env('DB_DATABASE', 'forge'),
        'username' => env('DB_USERNAME', 'forge'),
        'password' => env('DB_PASSWORD', ''),
        'unix_socket' => env('DB_SOCKET', ''),
        'charset' => 'utf8mb4',
        'collation' => 'utf8mb4_unicode_ci',
        'prefix' => '',
        'prefix_indexes' => true,
        'strict' => true,
        'engine' => null,
    ],
];

Finally, we use the config function in the code to obtain the database configuration. The following code is an example:

$dbConfig = config('database.mysql');

6. Summary

Configuring database connections in Laravel can be done through environment configuration files, database configuration files, supporting multiple database connections at the same time, and using special configuration files. to improve code readability. The above is a detailed introduction to Laravel database configuration. I hope it will be helpful to everyone.

The above is the detailed content of How to do database configuration 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