title: Laravel - Error: App\Exports\View must be compatible with Maatwebsite\Excel\Concerns\FromView::view(): Illuminate\Contracts\View\View
body: <p>I am trying to export to excel using PHP 7, Laravel 5.8, Maatwebsite Excel 3.1.  I successfully display on the view blade and also perform the filter.</p>
  
  <p>Model:
  use App\UserResponse;</p>
  
  <p><strong>Controller</strong></p>
  
  <pre class="lang-php prettyprint-override"><code>public function userresponseReport(Request $request,$export=false)
  {
      $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;
      }
  
      if(!empty($export))
      {
          return Excel::download(new UserresponseExport($userresponses-&gt;get()), 'userresponse.xlsx');
      }
      $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);
  }
  
  </code></pre>
  
  <p>Then after that, the view blade:</p>
  
  <p><strong>userresponseReport.blade.php</strong></p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><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('userresponseReport',['export']) }}" 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;
  
  
  &lt;div class="box box-primary"&gt;
          &lt;div class="box-header with-border"&gt;
  &lt;table class="table table-bordered table-hover table-striped table-condesed" id="commenter_info_table"&gt;
      &lt;caption&gt;&lt;/caption&gt;
      &lt;thead&gt;
          &lt;tr&gt;
              &lt;td&gt;#&lt;/td&gt;
              &lt;td&gt;Date&lt;/td&gt;
              &lt;td&gt;MSISDN&lt;/td&gt;
              &lt;td&gt;Game Code&lt;/td&gt;
              &lt;td&gt;Game Name&lt;/td&gt;
              &lt;td&gt;Answer&lt;/td&gt;
              &lt;td&gt;Channel&lt;/td&gt;
          &lt;/tr&gt;
      &lt;/thead&gt;
      &lt;tbody&gt;
          @foreach($userresponses as $key =&gt; $userresponse)
              &lt;tr&gt;
                  &lt;td&gt;{{ ++$key }}&lt;/td&gt;
  &lt;!--                &lt;td&gt;{{ $userresponse-&gt;created_date }}&lt;/td&gt;--&gt;
                  &lt;td&gt;{{ date('Y-m-d h:i:s A', strtotime($userresponse-&gt;created_date)) }}&lt;/td&gt;
                  &lt;td&gt;{{ $userresponse-&gt;msisdn }}&lt;/td&gt;
                  &lt;td&gt;{{ $userresponse-&gt;game_code }}&lt;/td&gt;
                  &lt;td&gt; 
                      @if($userresponse-&gt;game_code=='101')
                         Trivia
                      @elseif($userresponse-&gt;game_code=='102')
                         Predict and Win 
                      @elseif($userresponse-&gt;game_code=='103')
                         Party With the BBN 
                      @elseif($userresponse-&gt;game_code=='104')
                         Grand Prize  
                      @elseif($userresponse-&gt;game_code=='105')
                         Happy Hour    
                      @elseif($userresponse-&gt;game_code=='106')
                         Power Boost                         
                      @endif                       
                  &lt;/td&gt;                
                  &lt;td&gt;{{ $userresponse-&gt;answer }}&lt;/td&gt;
                  &lt;td&gt;{{ $userresponse-&gt;user_channel }}&lt;/td&gt;                                    
              &lt;/tr&gt;          
          @endforeach
              &lt;tr&gt;
              &lt;td colspan="14"&gt;
                  {{ $userresponses-&gt;links() }}
              &lt;/td&gt;
              &lt;/tr&gt;         
      &lt;/tbody&gt;
  
  &lt;/table&gt;   </code></pre>
  </div>
  </div>
  </p>
  
  <p>Then the Export</p>
  
  <p><strong>UserresponseExport</strong></p>
  
  <pre class="lang-php prettyprint-override"><code>
  class UserresponseExport implements FromView, WithHeadings, ShouldAutoSize, WithEvents, WithMapping
  {
      protected $userresponses;
  
      public function __construct($userresponses = null)
      {
          $this-&gt;userresponses = $userresponses;
      }
  
      public function view(): View
      {
          return view('report.userresponseReport', [
              'userresponses' =&gt; $this-&gt;userresponses ?: DB::table('user_response as g')
                  -&gt;select(
                      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')
          ]);
      }
  
      private $headings = [
          'Date Created',
          'MSISDN',
          'game_code',
          'Answer',
          'Channel'
      ];
      public function headings(): array
      {
          return $this-&gt;headings;
      }
  
  
      public function registerEvents(): array
      {
          return [
              AfterSheet::class    =&gt; function(AfterSheet $event) {
                  $cellRange = 'A1:E1'; // All headers
                  $event-&gt;sheet-&gt;getDelegate()-&gt;getStyle($cellRange)-&gt;getFont()-&gt;setSize(14);
  
              },
  
  
          ];
      }
  
  }
  </code></pre>
  
  <p><strong>Route</strong></p>
  
  <pre><code>Route::get('/report/userresponse-report/{export?}', ['as' =&gt; 'userresponseReport', 'uses' =&gt; 'ReportController@userresponseReport']);
  </code></pre>
  
  <p>On the view blade, when I clicked on search everything was okay. But when I click on export, I got this error:</p>
  
  <blockquote>
    <p>Symfony \ Component \ Debug \ Exception \ FatalErrorException (E_UNKNOWN)
    Declaration of App\Exports\UserresponseExport::view(): App\Exports\View must be compatible with Maatwebsite\Excel\Concerns\FromView::view(): Illuminate\Contracts\View\View</p>
  </blockquote>
  
  <p><a href="https://i.stack.imgur.com/McmGL.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/McmGL.png" alt="export_error"></a></p>
  
  <ol>
  <li>What could have caused this error?</li>
  <li>How do I resolve it?</li>
  </ol>