简体   繁体   English

Laravel交易-两种类型

[英]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? 在Laravel中,当涉及交易时,似乎有两种方法,这是不同的,并且是最常见的方法?

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

});

and other approach: 和其他方法:

DB::transaction(function () {

});

Actually laravel transaction can do in two ways 实际上,laravel交易可以通过两种方式完成

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. 绝对相同,因为您要在\\Illuminate\\Database\\Connection对象上调用transaction()方法。 These are just different approaches how you access Connection object, either through DB facade or through a model class. 这些是通过DB Facade或模型类访问Connection对象的不同方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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