title: Laravel - Adding Filter to Maatwebsite Excel Export
body: <p>I want to export only filtered data in view blade. I am using Laravel 5.8 and Maatwebsite 3.1 and PHP 7. </p>
  
  <p><a href="https://i.stack.imgur.com/dWSva.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/dWSva.png" alt="excel_export"></a></p>
  
  <p>Controller </p>
  
  <pre><code>    public function userresponseReport(Request $request)
  {
      $data['title'] = 'User Response';
  
      $userresponses = DB::table('user_response as g')
      -&gt;select(
         //DB::raw('DATE(g.created_at) as created_date'),
         DB::raw('g.created_at as created_date'),
         'g.msisdn', 
         'g.game_code', 
         'g.answer',
         'g.answer_code',
         'g.Amount_charged',
         'g.payment_ref',
         'g.status',
         'g.user_channel'                
    )               
   -&gt;orderByRaw('g.created_at DESC'); 
  
      $start_date = $request-&gt;start_date;
      $end_date = $request-&gt;end_date; 
  
      $render=[];  
      if(isset($request-&gt;start_date) &amp;&amp; isset($request-&gt;end_date))
      {
          $userresponses=$userresponses-&gt;whereBetween('created_at',[$start_date.' 00:00:00',$end_date.' 23:59:59']);
          $render['start_date']=$request-&gt;start_date;
          $render['end_date']=$request-&gt;end_date;
      }elseif(isset($request-&gt;start_date))
      {
          $userresponses=$userresponses-&gt;where('created_at',$request-&gt;start_date);
          $render['start_date']=$request-&gt;start_date;
      }        
      if(isset($request-&gt;msisdn))
      {
          $userresponses=$userresponses-&gt;where('msisdn','like','%'.$request-&gt;msisdn.'%');
          $render['msisdn']=$request-&gt;msisdn;
      }
      if(isset($request-&gt;game_code))
      {
          $userresponses=$userresponses-&gt;where('game_code','like','%'.$request-&gt;game_code.'%');
          $render['game_code']=$request-&gt;game_code;
      }   
      if(isset($request-&gt;user_channel))
      {
          $userresponses=$userresponses-&gt;where('user_channel','like','%'.$request-&gt;user_channel.'%');
          $render['user_channel']=$request-&gt;user_channel;
      }      
      $userresponses= $userresponses-&gt;orderBy('created_at','DESC');
      $userresponses= $userresponses-&gt;paginate(15);
      $userresponses= $userresponses-&gt;appends($render);
      $data['userresponses'] = $userresponses;
  
  return view('report.userresponseReport',$data);        
  }
  
  public function exportuserresponse(Request $request) 
  {       
      return Excel::download(new UserresponseExport($request), 'userresponse.xlsx');
  } 
  </code></pre>
  
  <p>I went through the documentation and applied this:</p>
  
  <p>Export folder: UserresponseExport</p>
  
  <pre><code>class UserresponseExport implements FromCollection, WithHeadings, ShouldAutoSize, WithEvents
  {
  use Exportable;
  
   protected $request;
  public function __construct($request)
  {
     $this-&gt;request = $request;
  } 
  
  public function collection()
  {
      $userresponses = UserResponse::where('msisdn', 'like','%'.$this-&gt;request-&gt;msisdn)
              -&gt;orWhere('game_code', 'like','%'.$this-&gt;request-&gt;game_code)
              -&gt;orWhere('user_channel', 'like','%'.$this-&gt;request-&gt;user_channel)
              -&gt;get();
      $output = [];
  
      foreach ($userresponses as $userresponse)
      {
        $output[] = [
          $userresponse-&gt;created_at,
            $userresponse-&gt;msisdn,
            $userresponse-&gt;game_code,
            $userresponse-&gt;answer,
            $userresponse-&gt;user_channel,
        ];
      }
      return collect($output);
  }  
  
  
  public function headings(): array
  {
      return [
      'Date Created',
      'MSISDN',
      'game_code',
      'Answer',
      'Channel'
      ];
  } 
  </code></pre>
  
  <p>View</p>
  
  <pre><code>    &lt;div class="row" style="margin-bottom: 10px"&gt;
      {{ Form::model(request(),['method'=&gt;'get']) }}
      &lt;div class="col-sm-2"&gt;
           {{ Form::text('msisdn',null,['class'=&gt;'form-control','placeholder'=&gt;'MSISDN']) }}
      &lt;/div&gt;
      &lt;div class="col-sm-2"&gt;
           {{ Form::text('game_code',null,['class'=&gt;'form-control','placeholder'=&gt;'Game Code']) }}
      &lt;/div&gt;   
      &lt;div class="col-sm-2"&gt;
           {{ Form::text('user_channel',null,['class'=&gt;'form-control','placeholder'=&gt;'Channel']) }}
      &lt;/div&gt;          
      &lt;div class="col-sm-2"&gt;
          {{ Form::date('start_date',null,['class'=&gt;'form-control','placeholder'=&gt;'Date']) }}
      &lt;/div&gt;
      &lt;div class="col-sm-2"&gt;
          {{ Form::date('end_date',null,['class'=&gt;'form-control','placeholder'=&gt;'Date']) }}
      &lt;/div&gt;          
      &lt;div class="col-xs-2"&gt;
          {{ Form::submit('Search',['class'=&gt;'btn btn-warning']) }}
          &lt;a href="{{ route('userresponseexport') }}" class="btn btn-primary"&gt;&lt;i class="fa fa-file-excel-o"&gt;&lt;/i&gt; Excel&lt;/a&gt;
      &lt;/div&gt;
      {{ Form::close() }}
  &lt;/div&gt;
  </code></pre>
  
  <p>IThe export submit button is sending everything to Excel. How do I make it to send only the filtered data.  Thanks</p>