PHP development framework Laravel database operation method summary, framework laravel

Source: Internet
Author: User

PHP development framework Laravel database operation method summary, framework laravel

1. read/write connections

Sometimes you may want to connect to a database using a SELECT statement, and the other to insert, update, and delete statements. Laravel makes this breeze and will always use the correct connection to determine whether to use the original query, Query Builder or eloquent ORM.

How to configure read/write connections? Let's take a look at this example:
Copy codeThe Code is as follows:
'Mysql' => array ('read' => array ('host' => '2017. 168.1.1 '), 'write' => array ('host' => '2017. 168.1.2 '), 'driver' => 'mysql', 'database' => 'database', 'username' => 'root', 'Password' => '', 'charset' => 'utf8', 'colation' => 'utf8 _ unicode_ci ', 'prefix' => '')

Note: The two keys are added to the configuration array: read and write. These two keys have an array value that contains a key: host. Other read/write Database options will be merged from the primary mysql connection array. Therefore, we only need to put the item into the Read and Write arrays if we want to overwrite the values in the main array. Therefore, in this case, 192.168.1.1 will be used as a "read" connection and while192.168.1.2 will be used as a "write" connection. Database creden, prefixes, character sets, and all other options are connected across two shared connections in the primary mysql array.

Ii. Run Query

Once you have configured a database connection, you can use the DB to run the Query Class.

Run a Select query

Copy codeThe Code is as follows:
$ Results = DB: select ('select * from users where id =? ', Array (1 ));

The result selection method always returns an array.

Run an Insert statement

Copy codeThe Code is as follows:
DB: insert ('insert into users (id, name) values (?, ?) ', Array (1, 'day '));

Run an update statement

Copy codeThe Code is as follows:
DB: update ('Update users set votes = 100 where name =? ', Array ('john '));

Run a Delete statement

Copy codeThe Code is as follows:
DB: delete ('delete from users ');

Note: The number of rows returned by the update and delete statements affects the operation.

Run a general statement

Copy codeThe Code is as follows:
DB: statement ('drop table users ');

Query event listening

You can query event listening using DB: Listener method:

Copy codeThe Code is as follows:
DB: listen (function ($ SQL, $ bindings, $ time ){//});

Iii. database transactions

A group of operations that run in a database transaction. You can use the transaction method:
Copy codeThe Code is as follows:
DB: transaction (function () {DB: table ('users')-> update (array ('votes'
=> 1); DB: table ('posts')-> delete ();});

Note: When any exception thrown by the transaction is closed, the automatic transaction will be rolled back.

Sometimes you may need to start a transaction:

Copy codeThe Code is as follows:
DB: beginTransaction ();

You can use the rollback transaction rollback method:

Copy codeThe Code is as follows:
DB: rollback ();

Finally, you can submit a transaction.

Copy codeThe Code is as follows:
DB: commit ();

Iv. Access Connection

When multiple connections are used, you can access them through the DB: connection method:

Copy codeThe Code is as follows:
$ Users = DB: connection ('foo')-> select (...);

You can also access the original and potential PDO instances:
Copy codeThe Code is as follows:
$ Pdo = DB: connection ()-> getPdo ();

Sometimes you may need to reconnect to a given database:
Copy codeThe Code is as follows:
DB: reconnect ('foo ');

If you need to disconnect from a given database, it will exceed the limit of the underlying PDO instance 'smax _ connections, use the disconnection method:
Copy codeThe Code is as follows:
DB: disconnect ('foo ');

5. query logs

By default, Laravel logs are stored in all queries in the memory to run the current request. However, in some cases, such as when the number of inserted rows, this may cause the application to use excess memory. Disable logs. You can use the disableQueryLog method:

Copy codeThe Code is as follows:
DB: connection ()-> disableQueryLog ();

O to get a group of executed queries, you can use the getQueryLog method:
Copy codeThe Code is as follows:
$ Queries = DB: getQueryLog ();


Php_laravel framework

In the 280907494 development group, many of them are engaged in this.

Who has the php source code framework (can read the database and add data to the background)

You go to the next dedecms
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.