【问题标题】:How do i know if a transaction went well in Laravel?我如何知道 Laravel 中的交易是否顺利?
【发布时间】:2016-02-17 15:27:07
【问题描述】:
假设我在 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?
【问题讨论】:
标签:
php
mysql
database
laravel
transactions
【解决方案1】:
除非某些组件在事务执行期间抛出异常,否则事务应该按预期工作
如果您想检查是否有特定的问题,您可以这样做:
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 会自己处理异常,所以如果你不需要做一些具体的事情,你可以假设你的交易是好的,如果在交易主体内没有引发错误