Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Which databases are supported by laravel

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces "which databases are supported by laravel". In daily operation, I believe many people have doubts about which databases are supported by laravel. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for you to answer the doubts about "which databases does laravel support?" Next, please follow the editor to study!

Laravel supports four databases: 1, MySQL, a relational database management system; 2, PostgreSQL, an "object-relational" database management system; 3, SQLite, a lightweight relational database management system; 4, SQL Server, a relational database management system.

The operating environment of this tutorial: windows7 system, Laravel6 version, Dell G3 computer.

Laravel supports native SQL queries, smooth query constructors, and Eloquent ORM, which make it easy to interact with databases in a variety of database backgrounds.

Currently, Laravel supports the following four databases:

MySQL 5.7 +: a relational database management system, developed by the Swedish company MySQL AB, is a product of Oracle.

PostgreSQL 9.6 database: a fully featured object-relational database management system for free software, which is based on the POSTGRES,4.2 version developed by the computer Department of the University of California.

SQLite 3.8.8 database: a lightweight database, an ACID-compliant relational database management system, contained in a relatively small C library.

SQL Server 2017+: a relational database management system launched by Microsoft

Configuration

The database configuration file is in the config/database.php file, where you can define all database connection configurations and specify the default database connection. Most examples of database configurations that Laravel can support are provided in this file.

By default, Laravel's sample environment is configured with Laravel Homestead (a small virtual machine that makes it easy for you to develop locally using Laravel). You can modify this configuration file according to the needs of the local database.

SQLite configuration

After creating a new SQLite database using create commands such as touch database/database.sqlite, you can use the absolute path of the database to configure environment variables to point to the newly created database:

DB_CONNECTION=sqliteDB_DATABASE=/absolute/path/to/database.sqlite

To enable foreign key constraints for SQLite connections, set the DB_foreign_KEYS environment variable to true:

DB_FOREIGN_KEYS=true

Configuration in URLs form

Typically, database connections use multiple configuration values, such as host, database, username, password, and so on. Each of these configuration values has its corresponding environment variable. This means that multiple environment variables need to be managed when configuring database connection information on the production server.

Some managed database providers, such as Heroku, provide a single database "URL" that contains all the connection information for the database in a single string. The sample database URL might look like this:

Mysql://root:password@127.0.0.1/forge?charset=UTF-8 these URLs usually follow the standard mode convention: driver://username:password@host:port/database?options

For convenience, Laravel supports these URLs as an alternative to configuring the database with multiple configuration options. If the url (or corresponding DATABASE_URL environment variable) configuration option exists, it is used to extract database connection and credential information.

Separation of reading and writing

Sometimes you want SELECT statements to use one database connection, while INSERT, UPDATE, and DELETE statements use another database connection. In Laravel, whether you use native queries, query constructors, or Eloquent ORM, you can easily implement them.

To understand how read-write separation is configured, let's look at an example:

'mysql' = > [' read' = > ['host' = > [' 192.168.1.1', '196.168.1.2',],], 'write' = > [' host' = > ['196.168.1.3',],], 'sticky' = > true,' driver' = > 'mysql' 'database' = >' database', 'username' = >' root', 'password' = >'', 'charset' = >' utf8mb4', 'collation' = >' utf8mb4_unicode_ci', 'prefix' = >',]

Note that in the above example, three keys have been added to the configuration array, namely read, write, and sticky. Both read and write contain an array with the key host. Other database options for read and write are in the array with the key mysql.

If you want to rewrite the configuration in the main array, you only need to modify the read and write arrays. So, in this example, 192.168.1.1 and 192.168.1.2 will connect to the host as "read", while 192.168.1.3 will connect to the host as "write". The two connections share various configurations of the mysql array, such as database credentials (username / password), prefixes, character encodings, and so on.

Sticky option

Sticky is an optional value that immediately reads records that have been written to the database during the current request cycle. If the sticky option is enabled and a write operation has been performed during the current request cycle, any read operation will use a write connection. This ensures that data written in the same request cycle can be read immediately, thus avoiding the problem of data inconsistency caused by master-slave synchronization delay. However, whether or not to enable it depends on the requirements of the application.

Use multiple database connections

When using multiple database connections, you can access each connection through the DB Facade facade's connection method. The parameter name passed to the connection method should be a value in the connections array in the config/database.php configuration file:

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

You can also use the getPdo method on a connection instance to access the underlying PDO instance:

$pdo = DB::connection ()-> getPdo ()

Execute native SQL queries

Once the database connection is configured, you can use the DB facade facade to run the query. DB facade provides methods for each type of query: select,update,insert,delete and statement.

Execute Select query

You can use DB Facade's select method to run the basic query:

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report