title: How to export Excel data with the date range in Laravel?
body: <p>With this code, I can search the data with the date range and it shows in the view file with the total count number. Now, I want to export the data with the date range using <a href="https://github.com/Maatwebsite/Laravel-Excel" rel="nofollow noreferrer">Maatwebsite/Laravel-Excel</a> package (from query) or any other good method.</p>
  
  <p>Can anyone please help me with this? </p>
  
  <p><code>Blade View</code></p>
  
  <pre class="lang-html prettyprint-override"><code>&lt;div class="panel-heading"&gt;
      &lt;div class="row"&gt;
          &lt;div class="col-md-5"&gt;Total Records - &lt;b&gt;&lt;span id="total_records"&gt;&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;
          &lt;div class="col-md-5"&gt;
              &lt;div class="input-group input-daterange"&gt;
                  &lt;input type="text" name="from_date" id="from_date" readonly class="form-control" /&gt;
                  &lt;div class="input-group-addon"&gt;to&lt;/div&gt;
                  &lt;input type="text" name="to_date" id="to_date" readonly class="form-control" /&gt;
              &lt;/div&gt;
          &lt;/div&gt;
          &lt;div class="col-md-2"&gt;
              &lt;button type="button" name="filter" id="filter" class="btn btn-info btn-sm"&gt;Filter&lt;/button&gt;
              &lt;button type="button" name="refresh" id="refresh" class="btn btn-warning btn-sm"&gt;Refresh&lt;/button&gt;
          &lt;/div&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  
  &lt;table class="table table-striped table-bordered"&gt;
      &lt;thead&gt;
          &lt;tr&gt;
              &lt;th width="6%"&gt;Owner Manager&lt;/th&gt;
              &lt;th width="6%"&gt;CSR Name&lt;/th&gt;
              &lt;th width="6%"&gt;CSR EID&lt;/th&gt;
          &lt;/tr&gt;
      &lt;/thead&gt;
  &lt;/table&gt;
  </code></pre>
  
  <p><code>JQuery Code</code></p>
  
  <pre><code>$(document).ready(function () {
      var date = new Date();
  
      $('.input-daterange').datepicker({
          todayBtn: 'linked',
          format: 'yyyy-mm-dd',
          autoclose: true
      });
  
      var _token = $('input[name="_token"]').val();
  
      fetch_data();
  
      function fetch_data(from_date = '', to_date = '') {
          $.ajax({
              url: "{{ route('dr.fetch_data') }}",
              method: "POST",
              data: {
                  from_date: from_date,
                  to_date: to_date,
                  _token: _token
              },
              dataType: "json",
              success: function (data) {
                  var output = '';
                  $('#total_records').text(data.length);
                  for (var count = 0; count &lt; data.length; count++) {
                      output += '&lt;tr&gt;';
                      output += '&lt;td&gt;' + data[count].owner_manager + '&lt;/td&gt;';
                      output += '&lt;td&gt;' + data[count].csr_name + '&lt;/td&gt;';
                      output += '&lt;td&gt;' + data[count].csr_eid + '&lt;/td&gt;';
                  }
                  $('tbody').html(output);
              }
          })
      }
  
      $('#filter').click(function () {
          var from_date = $('#from_date').val();
          var to_date = $('#to_date').val();
          if (from_date != '' &amp;&amp; to_date != '') {
              fetch_data(from_date, to_date);
          } else {
              alert('Both Date is required');
          }
      });
  
      $('#refresh').click(function () {
          $('#from_date').val('');
          $('#to_date').val('');
          fetch_data();
      });
  });
  </code></pre>
  
  <p><code>Controller Code</code></p>
  
  <pre class="lang-php prettyprint-override"><code>namespace App\Http\Controllers;
  
  use Illuminate\Http\Request;
  use Illuminate\Support\Facades\DB;
  use App\Http\Controllers\Controller;
  
  class DRController extends Controller
  {
      function index()
      {
          return view('dr');
      }
  
      function fetch_data(Request $request)
      {
          if ($request-&gt;ajax()) {
              if ($request-&gt;from_date != '' &amp;&amp; $request-&gt;to_date != '') {
                  $data = DB::table('users_info')-&gt;whereBetween('ptdate', array($request-&gt;from_date, $request-&gt;to_date))-&gt;get();
              } else {
                  $data = DB::table('users_info')-&gt;orderBy('ptdate', 'desc')-&gt;get();
              }
              echo json_encode($data);
          }
      }
  }
  </code></pre>