title: How to send collection as an excel file in laravel
body: <p>I'm using Laravel Excel 3.1 from <a href="https://laravel-excel.maatwebsite.nl/" rel="nofollow noreferrer">https://laravel-excel.maatwebsite.nl/</a> and I am trying to figure out if there is a simple way to simply render a collection as an excel file.</p>
  
  <p>In my controller, I have code that generates a db query and puts it into a view. I'd like to give the user the ability to download the data in that view as an excel document. The code used to create the view is not simple, and it is based on many query inputs. </p>
  
  <p>In my <code>PatientsController.php</code>, I have</p>
  
  <pre><code>public function excelExport(Request $request)
  {
      $params = $this-&gt;getQueryParams($request);
      $pts = $this-&gt;createIndexCollection($request, $params);
  
      return Excel::download(new PatientsExport, 'patients.xlsx');
  }
  </code></pre>
  
  <p>I'd like to pass the already-computed collection <code>$pts</code> to the download command. Is this possible?</p>
  
  <p><code>PatientsExport</code> is copied straight from the docs.</p>