use Illuminate\Support\Facades\DB; use App\Http\Model\User; $data = [ 'nickname' => 'test', 'gender' => 0, ]; $data2 = [ 'nickname' => 'test2', ]; DB::beginTransaction(); //开启事务 User::insertGetId($data); //插入数据 //捕捉异常 try { $id = User::insertGetId($data2); //插入第二条数据并提交 DB::commit(); }catch (\Exception $e){ DB::rollBack(); //异常则回滚并报错 return response()->json(['error',$e->getMessage()]); }