Home >PHP Framework >Laravel >Where is laravel's db?
In Laravel, DB refers to the database, which is generally stored in relational databases such as MySQL, PostgreSQL, and SQLite. So, where is the DB in the Laravel framework?
Database configuration in Laravel framework
In Laravel, the database configuration file is located at /config/database.php, and its default configuration is as follows:
'default' => ; env('DB_CONNECTION', 'mysql'),
'connections' => [
'sqlite' => [ 'driver' => 'sqlite', 'url' => env('DATABASE_URL'), 'database' => env('DB_DATABASE', database_path('database.sqlite')), 'prefix' => '', 'foreign_key_constraints' => env('DB_FOREIGN_KEYS', true), ], 'mysql' => [ 'driver' => 'mysql', 'url' => env('DATABASE_URL'), 'host' => env('DB_HOST', '127.0.0.1'), '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, 'options' => extension_loaded('pdo_mysql') ? array_filter([ PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'), ]) : [], ], 'pgsql' => [ 'driver' => 'pgsql', 'url' => env('DATABASE_URL'), 'host' => env('DB_HOST', '127.0.0.1'), 'port' => env('DB_PORT', '5432'), 'database' => env('DB_DATABASE', 'forge'), 'username' => env('DB_USERNAME', 'forge'), 'password' => env('DB_PASSWORD', ''), 'charset' => 'utf8', 'prefix' => '', 'prefix_indexes' => true, 'schema' => 'public', 'sslmode' => 'prefer', ], 'sqlsrv' => [ 'driver' => 'sqlsrv', 'url' => env('DATABASE_URL'), 'host' => env('DB_HOST', 'localhost'), 'port' => env('DB_PORT', '1433'), 'database' => env('DB_DATABASE', 'forge'), 'username' => env('DB_USERNAME', 'forge'), 'password' => env('DB_PASSWORD', ''), 'charset' => 'utf8', 'prefix' => '', 'prefix_indexes' => true, ],
],
In the default configuration, default represents the default database The connection type is mysql, and the subarray under connections defines four database connection methods: sqlite, mysql, pgsql, and sqlsrv. In Laravel, we can add more database link methods according to our needs.
DB interface in the Laravel framework
The Laravel framework provides us with a lot of DB interfaces, the most important of which is IlluminateDatabaseEloquentModel, which provides us with the ability to query the database and Basic methods of operation, such as: all(), create(), update(), where(), orderBy(), etc.
When using Laravel for database operations, we need to introduce the DB interface first. The usual writing method is:
use IlluminateDatabaseEloquentModel;
Then we can operate the database, For example:
$user = User::find(1);
$user->name = 'new name';
$user->save();
The above code updates the name of the user with ID 1.
DB implementation in the Laravel framework
In the Laravel framework, the implementation of DB is based on PDO (PHP Data Objects). PDO is a lightweight database abstraction layer in PHP that is used to encapsulate different Differences between databases. PDO abstracts away the syntax differences between different databases, allowing us to use the same code to operate on different databases.
When using DB in the Laravel framework, the database is actually operated through PDO. What should be noted here is that the PDO used in the Laravel framework is not the PDO that comes with PHP, but the PDO extension implemented by Laravel itself. It does some expansion and encapsulation of the native PDO, providing an easier-to-use and more flexible API.
The above is the relevant content of DB in the Laravel framework, including database configuration, DB interface and DB implementation, etc. When developing using Laravel, we can flexibly configure and use DB according to our needs to complete various complex database operations.
The above is the detailed content of Where is laravel's db?. For more information, please follow other related articles on the PHP Chinese website!