在Laravel中,如何知道交易是否成功?

3

假设我正在使用Laravel中的事务:

DB::transaction(function() {

     // Do your SQL here

});

有没有办法让我知道交易是否成功?

DB::transaction(function() {

     // Do your SQL here

});

// Here how can I know if the transaction is ok?
1个回答

2

在事务执行期间,除非某个组件抛出异常,否则事务应按预期工作。

如果您想检查是否发生了特定的错误,则可以执行以下操作:

try
{
    DB::transaction(function() {

     // Do your SQL here

    });

    //at this point everything is OK if no exceptions is raised
}
catch( PDOException $e )
{
    //something went wrong with mysql: handle your exception
}
catch( Exception $e )
{
    //something went wrong with some other component
}

但是请记住,Laravel将自己处理异常,所以如果您不需要执行特定的操作,只要在事务体内没有引发错误,就可以认为您的事务是成功的。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接