Laravel筛选的Excel View刀片将所有内容导出到Excel

时间:2019-07-24 09:54:43

标签: laravel export maatwebsite-excel

我正在使用Laravel 5.8和“ maatwebsite / excel”:“ ^ 3.1”。当我从视图刀片过滤文件时,它的性能很好。它根据选定的条件显示。

控制器

public function userresponseReport(Request $request, $export=false)
{
    $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'); 

    $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(), '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
{
use Exportable;

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

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

查看刀片(userresponseReport.blade.php)

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

我已经有好几天了。现在的问题是如何使它仅导出过滤的数据,而不导出表中的所有数据。

我需要帮助

0 个答案:

没有答案