Laravel-错误:App \ Exports \ View必须与Maatwebsite \ Excel \ Concerns \ FromView :: view()兼容:Illuminate \ Contracts \ View \ View

时间:2019-07-29 08:27:15

标签: laravel maatwebsite-excel

我正在尝试使用PHP 7,Laravel 5.8,Maatwebsite Excel 3.1导出到excel。我成功地在视图刀片上显示并执行过滤器。

型号: 使用App \ UserResponse;

控制器

public function userresponseReport(Request $request,$export=false)
{
    $data['title'] = 'User Response';

    $userresponses = DB::table('user_response as g')
        ->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'
        )
        ->orderByRaw('g.created_at DESC');

    $start_date = $request->start_date;
    $end_date = $request->end_date;

    $render=[];
    if(isset($request->start_date) && isset($request->end_date))
    {
        $userresponses=$userresponses->whereBetween('created_at',[$start_date.' 00:00:00',$end_date.' 23:59:59']);
        $render['start_date']=$request->start_date;
        $render['end_date']=$request->end_date;
    }elseif(isset($request->start_date))
    {
        $userresponses=$userresponses->where('created_at',$request->start_date);
        $render['start_date']=$request->start_date;
    }
    if(isset($request->msisdn))
    {
        $userresponses=$userresponses->where('msisdn','like','%'.$request->msisdn.'%');
        $render['msisdn']=$request->msisdn;
    }
    if(isset($request->game_code))
    {
        $userresponses=$userresponses->where('game_code','like','%'.$request->game_code.'%');
        $render['game_code']=$request->game_code;
    }
    if(isset($request->user_channel))
    {
        $userresponses=$userresponses->where('user_channel','like','%'.$request->user_channel.'%');
        $render['user_channel']=$request->user_channel;
    }

    if(!empty($export))
    {
        return Excel::download(new UserresponseExport($userresponses->get()), 'userresponse.xlsx');
    }
    $userresponses= $userresponses->orderBy('created_at','DESC');
    $userresponses= $userresponses->paginate(15);
    $userresponses= $userresponses->appends($render);
    $data['userresponses'] = $userresponses;

    return view('report.userresponseReport',$data);
}

然后是视图刀片:

userresponseReport.blade.php

    <div class="row" style="margin-bottom: 10px">
        {{ Form::model(request(),['method'=>'get']) }}
        <div class="col-sm-2">
             {{ Form::text('msisdn',null,['class'=>'form-control','placeholder'=>'MSISDN']) }}
        </div>
        <div class="col-sm-2">
             {{ Form::text('game_code',null,['class'=>'form-control','placeholder'=>'Game Code']) }}
        </div>   
        <div class="col-sm-2">
             {{ Form::text('user_channel',null,['class'=>'form-control','placeholder'=>'Channel']) }}
        </div>          
        <div class="col-sm-2">
            {{ Form::date('start_date',null,['class'=>'form-control','placeholder'=>'Date']) }}
        </div>
        <div class="col-sm-2">
            {{ Form::date('end_date',null,['class'=>'form-control','placeholder'=>'Date']) }}
        </div>          
        <div class="col-xs-2">
            {{ Form::submit('Search',['class'=>'btn btn-warning']) }}
            <a href="{{ route('userresponseReport',['export']) }}" class="btn btn-primary"><i class="fa fa-file-excel-o"></i> Excel</a>
        </div>
        {{ Form::close() }}
    </div>


<div class="box box-primary">
        <div class="box-header with-border">
<table class="table table-bordered table-hover table-striped table-condesed" id="commenter_info_table">
    <caption></caption>
    <thead>
        <tr>
            <td>#</td>
            <td>Date</td>
            <td>MSISDN</td>
            <td>Game Code</td>
            <td>Game Name</td>
            <td>Answer</td>
            <td>Channel</td>
        </tr>
    </thead>
    <tbody>
        @foreach($userresponses as $key => $userresponse)
            <tr>
                <td>{{ ++$key }}</td>
<!--                <td>{{ $userresponse->created_date }}</td>-->
                <td>{{ date('Y-m-d h:i:s A', strtotime($userresponse->created_date)) }}</td>
                <td>{{ $userresponse->msisdn }}</td>
                <td>{{ $userresponse->game_code }}</td>
                <td> 
                    @if($userresponse->game_code=='101')
                       Trivia
                    @elseif($userresponse->game_code=='102')
                       Predict and Win 
                    @elseif($userresponse->game_code=='103')
                       Party With the BBN 
                    @elseif($userresponse->game_code=='104')
                       Grand Prize  
                    @elseif($userresponse->game_code=='105')
                       Happy Hour    
                    @elseif($userresponse->game_code=='106')
                       Power Boost                         
                    @endif                       
                </td>                
                <td>{{ $userresponse->answer }}</td>
                <td>{{ $userresponse->user_channel }}</td>                                    
            </tr>          
        @endforeach
            <tr>
            <td colspan="14">
                {{ $userresponses->links() }}
            </td>
            </tr>         
    </tbody>

</table>   

然后导出

UserresponseExport


class UserresponseExport implements FromView, WithHeadings, ShouldAutoSize, WithEvents, WithMapping
{
    protected $userresponses;

    public function __construct($userresponses = null)
    {
        $this->userresponses = $userresponses;
    }

    public function view(): View
    {
        return view('report.userresponseReport', [
            'userresponses' => $this->userresponses ?: DB::table('user_response as g')
                ->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'
                )
                ->orderByRaw('g.created_at DESC')
        ]);
    }

    private $headings = [
        'Date Created',
        'MSISDN',
        'game_code',
        'Answer',
        'Channel'
    ];
    public function headings(): array
    {
        return $this->headings;
    }


    public function registerEvents(): array
    {
        return [
            AfterSheet::class    => function(AfterSheet $event) {
                $cellRange = 'A1:E1'; // All headers
                $event->sheet->getDelegate()->getStyle($cellRange)->getFont()->setSize(14);

            },


        ];
    }

}

路线

Route::get('/report/userresponse-report/{export?}', ['as' => 'userresponseReport', 'uses' => 'ReportController@userresponseReport']);

在视图刀片上,当我单击“搜索”时,一切正常。但是当我单击导出时,出现此错误:

  

Symfony \组件\调试\异常\ FatalErrorException(E_UNKNOWN)   App \ Exports \ UserresponseExport :: view()的声明:App \ Exports \ View必须与Maatwebsite \ Excel \ Concerns \ FromView :: view():Illuminate \ Contracts \ View \ View

兼容

export_error

  1. 什么可能导致此错误?
  2. 我该如何解决?

1 个答案:

答案 0 :(得分:1)

此错误表明您的课程App\Exports\UserresponseExport未正确遵循该界面。

由于该错误,我们可以看到您需要拥有一个名为view的方法,但是您的方法已经将App\Exports\View而不是Illuminate\Contracts\View\View用作返回类型。

要解决此问题,只需将您的view方法的返回类型更改为Illuminate\Contracts\View\View


您的代码现在很可能会说

public function view(): View 
{
  ...
}

但是当您在导入语句中缺少use Illuminate\Contracts\View\View;时,View被解析为您的类的当前名称空间+您尝试键入的类,这导致App\Exports\View

因此,解决此问题而不是提示整个名称空间的另一种方法是在您的类中通过在文件顶部添加Illuminate\Contracts\View\View来导入use Illuminate\Contracts\View\View;