Coffee
Coffee

Reputation: 2223

Laravel mysql multiple operations with one DB instance

As far as I understand, each DB:: in Laravel (I use 5.2, if that matters) basically opens new connection and closes it when operation is finished.

The question is, is there any possibility to create one DB instance and do n operations using it? Something like native php, where you open connection and close it manually after everything is done.

Like

$conn = DB;
$users = $conn->table('users')->get();
$bills = $conn->table('bills')->lists('id');
/* something happens */
unset($conn);

Or something of this type.

Upvotes: 1

Views: 283

Answers (1)

EddyTheDove
EddyTheDove

Reputation: 13259

Use DB transactions

DB::transaction(function () {
    $users = DB::table('users')->get();
    $bills = DB::table('bills')->lists('id');
});

More here: https://laravel.com/docs/5.4/database#database-transactions

Upvotes: 2

Related Questions