简体   繁体   中英

Laravel transactions - two type

In Laravel, it appear there are two approach when it come to transaction, which is the different and which is most common approach?

return $this->model->getConnection()->transaction(function () use ($self, $request) {

});

and other approach:

DB::transaction(function () {

});

Actually laravel transaction can do in two ways

First Method: Handle transaction and rollback on you requirements

try {

   DB::beginTransaction();
   //Db trnscations
} catch (\Exception $e) {
   DB::rollBack()
}

Second Method: It will handle transaction and rollback it self

DB::transaction(function () {

});

It's absolutely the same, because you're calling transaction() method on \\Illuminate\\Database\\Connection object. These are just different approaches how you access Connection object, either through DB facade or through a model class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM