Laravel-错误:方法Illuminate \ Support \ Collection :: links不存在

时间:2019-07-29 09:22:25

标签: laravel maatwebsite-excel

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

型号:

use 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']);

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

  

ErrorException(E_ERROR)   方法Illuminate \ Support \ Collection :: links不存在。 (视图:C:\ xampp \ htdocs \ bbnaijareport \ resources \ views \ report \ userresponseReport.blade.php)   以前的例外   方法Illuminate \ Support \ Collection :: links不存在。 (0)

excel_report_error

如何解决此问题?

新错误 new_error

1 个答案:

答案 0 :(得分:0)

那是因为您正在userresponseReport.blade.php中显示分页,但是没有在导出部件中加载它。因此,也可以对导出功能进行分页或对链接进行检查:

@if($userresponses instanceof \Illuminate\Pagination\AbstractPaginator )
    <tr>
        <td colspan="14">
            {{ $userresponses->links() }}
        </td>
    </tr>  
@endif

Credits to Richie