title: Laravel database transactions aren't working
body: <p>I'm trying to setup a database transaction in <strong>Laravel 5.5,</strong> but it does not seem to work. I use <strong>MySQL 5.7.20, and all tables from the <em>tools</em> schema are InnoDB</strong>. I'm also running <strong>PHP 7.2.3</strong>.</p>
  
  <p>I have this code:</p>
  
  <pre><code>DB::beginTransaction();
  try {
      // checks whether the users marked for removal are already assigned to the list
      foreach ($removeStudents as $removeStudent) {
          if ($ls-&gt;allocatedStudents-&gt;find($removeStudent-&gt;id) === null) {
              throw new Exception('userNotAllocated', $removeStudent-&gt;id);
          } else {
              DB::connection('tools')-&gt;table('exercises_list_allocations')
                  -&gt;where('user_id', $removeStudent-&gt;id)
                  -&gt;where('exercises_list_id', $ls-&gt;id)
                  -&gt;delete();
          }
      }
  
      // checks whether the users marked for removal are already assigned to the list
      foreach ($addStudents as $addStudent) {
          if ($ls-&gt;allocatedStudents-&gt;find($addStudent-&gt;id) === null) {
              DB::connection('tools')-&gt;table('exercises_list_allocations')
                 -&gt;insert([
                     'user_id' =&gt; $addStudent-&gt;id,
                     'exercises_list_id' =&gt; $ls-&gt;id
                 ]);
          } else {
              throw new Exception('userAlreadyAllocated', $addStudent-&gt;id);
          }
      }
  
      DB::commit();
  } catch (Exception $e) {
      DB::rollBack();
      return response()-&gt;json(
          [
              'error' =&gt; $e-&gt;getMessage(),
              'user_id' =&gt; $e-&gt;getCode()
          ], 400
      );
  }
  </code></pre>
  
  <p>And it does not rollback the transaction. If an exception is found after some deletes or inserts, they are not reverted.</p>
  
  <p>At first I thought it coulde be an issue in MySQL, so I tried to manually run the following SQL queries:</p>
  
  <pre><code>START TRANSACTION;
  DELETE FROM tools.exercises_list_allocations WHERE user_id = 67 AND exercises_list_id=308;
  DELETE FROM tools.exercises_list_allocations WHERE user_id = 11479 AND exercises_list_id=308;
  INSERT INTO tools.exercises_list_allocations (user_id, exercises_list_id) VALUES (1,308);
  INSERT INTO tools.exercises_list_allocations (user_id, exercises_list_id) VALUES (2,308);
  INSERT INTO tools.exercises_list_allocations (user_id, exercises_list_id) VALUES (3,308);
  ROLLBACK;
  </code></pre>
  
  <p>And it rolls back all deletes and all inserts (as expected), no changes happening to the <em>tools.exercises_list_allocations</em> table. So, I ruled out the database server as being the problem.</p>
  
  <p>So, I figured it should be something with the PHP code. I searched the web for issues similar to mine, and tried some reported solutions.</p>
  
  <p>I tried to use the DB::transaction() method with an anonymous function instead of a try/catch block, to no success.</p>
  
  <p>I tried to use the Eloquent ORM instead of the DB::insert() and DB::delete() methods, both trying the DB::transaction() method with an anonymous function and DB::beginTransaction(), DB::commit() and DB::rollBack() methods, to no success.</p>
  
  <p>I tried disabling stric mode and forcing the engine to be InnoDB in config/database.php, to no success.</p>
  
  <p>What am I doing wrong? I need all the deletes and inserts to be run in a single atomic transaction.</p>