Laravel Transaction:laravel's transaction is not supported by eloquent, use DB:: The Way

Source: Internet
Author: User

Database Transaction Processing #

You can use the transaction method to perform a set of database transaction operations:

Db::Transaction(function(){Db::Table(' Users ')->update ([ ' votes ' => 1]) ; db::table ( ' posts ' ->delete (;}         

Note: transaction any exception thrown in the closure causes the transaction to be automatically rolled back.

Sometimes you may need to start a business yourself:

DB::beginTransaction();

rollbackthe way you can roll back a transaction:

DB::rollback();

Finally, you can commit commit the transaction by means of the method:

DB::commit();

Get Connection #

To use multiple connections, you can DB::connection take the method:

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

You can also take the original underlying PDO instance:

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

Sometimes you may need to reconnect to a specific database:

DB::reconnect(‘foo‘);

If you need to close a particular database connection because you are exceeding the limits of the underlying PDO instance, max_connections you can use the disconnect method:

DB::disconnect(‘foo‘);

Find Log Records #

Laravel can access all the lookup statements in this request in memory. In some cases, however, it is important to note that adding a large amount of data at a time can cause your application to wear too much memory. If you want to enable logging, you can use the enableQueryLog method:

DB::connection()->enableQueryLog();

To get an array of found records executed, you can use the getQueryLog method:

$queries = DB::getQueryLog();

Laravel Transaction:laravel's transaction is not supported by eloquent, use DB:: The Way

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.