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();
rollback
the 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