title: Laravel DB::transaction() return value
body: <p>It's my first time to use <code>DB::transaction()</code> but how exactly does it work if a transaction fails or is successful? In the example below, do I have to manually assign a value to return <code>true</code>, or if it fails will the method either return <code>false</code> or totally exit the transaction (therefore skipping the rest of the code)? The docs aren't so helpful on this.</p>
  
  <pre><code>use Exception;
  use DB;
  
  try {
      $success = DB::transaction(function() {
          // Run some queries
      });
  
      print_r($success);
  
  } catch(Exception $e) {
      echo 'Uh oh.';
  }
  </code></pre>
  
  <h3>Solution</h3>
  
  <p>I wrote down this solution for others who might be wondering.</p>
  
  <p>Since I was more concerned about returning a boolean value depending on the success of my query, with a few modifications it now returns <code>true/false</code> depending on its success:</p>
  
  <pre><code>use Exception;
  use DB;
  
  try {
    $exception = DB::transaction(function() {
      // Run queries here
    });
  
    return is_null($exception) ? true : $exception;
  
  } catch(Exception $e) {
      return false;
  }
  </code></pre>
  
  <p>Take note that the variable <code>$exception</code> is never returned since if something goes wrong with your query, the <code>catch</code> is immediately triggered returning <code>false</code>. Thanks to @ilaijin for showing that an <code>Exception</code> object is thrown if something goes wrong.</p>