gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 16:28:38 26 4
gpt4 key购买 nike

我正在尝试使用 PHP 7、Laravel 5.8、Maatwebsite Excel 3.1 导出到 excel。我成功地在 View Blade 上显示并执行过滤器。

模型:
使用 App\UserResponse;

Controller

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);
}


然后在此之后, View Blade :

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>


然后导出

用户响应导出


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

在 View Blade 上,当我点击搜索时一切正常。但是当我单击导出时,出现此错误:

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



export_error
  • 什么可能导致此错误?
  • 我该如何解决?
  • 最佳答案

    此错误表示您的类 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 , 在你的类(class)中加入 use Illuminate\Contracts\View\View;在文件的顶部。

    关于Laravel - 错误:App\Exports\View 必须与 Maatwebsite\Excel\Concerns\FromView::view(): Illuminate\Contracts\View\View 兼容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57249864/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com