我正在尝试在Laravel 5.5 中设置数据库事务,但它似乎不起作用。我使用MySQL 5.7.20,工具模式中的所有表都是 InnoDB。我也在运行PHP 7.2.3。
我有这个代码:
DB::beginTransaction();
try {
// checks whether the users marked for removal are already assigned to the list
foreach ($removeStudents as $removeStudent) {
if ($ls->allocatedStudents->find($removeStudent->id) === null) {
throw new Exception('userNotAllocated', $removeStudent->id);
} else {
DB::connection('tools')->table('exercises_list_allocations')
->where('user_id', $removeStudent->id)
->where('exercises_list_id', $ls->id)
->delete();
}
}
// checks whether the users marked for removal are already assigned to the list
foreach ($addStudents as $addStudent) {
if ($ls->allocatedStudents->find($addStudent->id) …Run Code Online (Sandbox Code Playgroud)