表必須是InnoDB引擎
DB::beginTransaction();
try{
$name='abc';
$result1= Test::create(['name'=>$name]);
if(!$result1) {
/**
* Exception類接收的參數
* $message = "", $code = 0, Exception $previous = null
*/
thrownew\Exception("1");
}
$result2= Test::create(['name'=>$name]);
if(!$result2) {
thrownew\Exception("2");
}
DB::commit();
}catch(\Exception$e){
DB::rollback();//事務回滾
echo$e->getMessage();
echo$e->getCode();
}