Laravel-传递给App \ Exports \ UserresponseExport :: __ construct()的参数1必须为字符串类型

时间:2019-07-23 14:05:59

标签: laravel maatwebsite-excel

我正在使用Laravel 5.8和Maatwebsites 3.1从视图刀片导出到excel。这很完美。问题出在哪里,我只想导出过滤后的数据,而不导出表中的所有内容。请参见下面的控制器:

控制器

    public function userresponseReport(Request $request,$export=false)
{        
    $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.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($request->msisdn, $request->game_code,$request->user_channel,$request->start_date,$request->end_date), 'userresponse.xlsx');
    }        
    $userresponses= $userresponses->orderBy('created_at','DESC');
    $userresponses= $userresponses->paginate(15);
    $userresponses= $userresponses->appends($render);
    $data['userresponses'] = $userresponses;

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

导出功能

class UserresponseExport implements FromQuery, WithHeadings, ShouldAutoSize, WithEvents

{

public function query()
{
    return UserResponse::query()->select(
    DB::raw("DATE(created_at)"),
    'msisdn',
    'game_code',
    'answer',
    'user_channel'
            );
}    

public function headings(): array
{
    return [
    'Date Created',
    'MSISDN',
    'game_code',
    'Answer',
    'Channel'
    ];
} 

public function __construct(string $msisdn, string $game_code, string $user_channel, string $start_date, string $end_date){
    $this->msisdn = $msisdn;
    $this->game_code = $game_code;
    $this->user_channel = $user_channel;
    $this->start_date = $start_date;
    $this->end_date = $end_date;
}    

查看刀片

        {{ 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() }}

当我尝试仅导出过滤后的数据时,出现此错误

  

传递给App \ Exports \ UserresponseExport :: __ construct()的参数1必须为字符串类型,给定为null,在第198行的C:\ xampp \ htdocs \ bbnaijareport \ app \ Http \ Controllers \ ReportController.php中调用

这是第198行:

return Excel::download(new UserresponseExport($request->msisdn, $request->game_code,$request->user_channel,$request->start_date,$request->end_date), 'userresponse.xlsx');

excel_error

如何解决此错误?还是有其他更好的方法可以做到这一点。

1 个答案:

答案 0 :(得分:0)

更改控制器以将null自动转换为空字符串

return Excel::download(new UserresponseExport($request->msisdn ?? '', ...