我们都使用DB :: transaction()来进行多次插入查询。在这样做的时候,应该尝试…抓住放在里面或包装吗?甚至有必要包括一个try … catch当一个事务会自动失败,如果出了问题?
示例try …抓住包装事务:
// try...catch
try {
// Transaction
$exception = DB::transaction(function() {
// Do your SQL here
});
if(is_null($exception)) {
return true;
} else {
throw new Exception;
}
}
catch(Exception $e) {
return false;
}
相反,一个DB :: transaction()包装一个try … catch:
// Transaction
$exception = DB::transaction(function() {
// try...catch
try {
// Do your SQL here
}
catch(Exception $e) {
return $e;
}
});
return is_null($exception) ? true : false;
或者简单的一个交易w / o一个尝试…抓住
// Transaction only
$exception = DB::transaction(function() {
// Do your SQL here
});
return is_null($exception) ? true : false;