title: Laravel - Get variable from a DB Transaction Closure
body: <p>I am working with a Laravel 5 LAMP stack, and I am trying to process a CSV import with a database transaction. Code looks like this:</p>
  
  <pre><code>// Should contain any messages to pass back to the user
  $results = [];
  
  // Contains the total records inserted
  $total = 0;
  
  DB::transaction(function() use($csv_file, $results, $total) {
  
      // Some Code ...
  
      $total++;
      $results[] = 'Row 10 has some weird data...';
  });
  
  return view('plan.import')
      -&gt;with('results', $results)
      -&gt;with('total', $total);
  </code></pre>
  
  <p>At the end of that, my records are imported, but my $total and $results are still empty, since they are outside the scope of the closure.  I know they are being altered inside the function, because I've stepped through it, and seen them change.  I just can't figure how to get them out of that transaction and return them to the user.  Can anyone please help with this?</p>