我在刀片视图中添加了一个导出按钮,但是我得到了以下错误。
函数App\Http\Controllers\TripController::export(),0传递的参数太少,而且确切地说是1预期的
这是我想要导出到excel文档的HTML表

TripsExport.php
<?php
namespace App\Exports;
use App\Trip;
use Illuminate\Contracts\View\View;
use Maatwebsite\Excel\Concerns\FromView;
class TripsExport implements FromView
{
public function view(): View
{
return view('trips.edit', [
'events' => Events::all()
]);
}
}trip.edit.blade.php
@extends('layouts.app')
@section('content')
<div class="container-fluid padding">
<div class="row welcome text-center">
<div class="col-12">
<h1 class="display-6"> {{ $trip->destination }} Itinerary </h1>
</div>
</div>
<div class="row welcome text-center">
<div class="col-12">
<h4 class="display-8"> {{$trip->startdate}} - {{$trip->enddate}}</h4>
</div>
</div>
</div>
<hr>
<div class="row text-center">
<div class="col-12">
<a href="/trips" class="btn btn-secondary">Go back</a>
<a href="/trips/{{$trip->id}}/edit/AddEvent" class="btn btn-success"> Add to Itinerary</a>
<a href="{{route('export')}}" class="btn btn-secondary">Export</a>
</div>
</div>
<br/>
<table class="table table-striped table-bordered table-hover">
<thead class="thead">
<tr class="warning">
<th>Category</th>
<th>Event Name</th>
<th>Start Date</th>
<th>End Date</th>
<th>Time</th>
<th>Address</th>
<th>Notes</th>
<th>Edit</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
@foreach($trip->events as $event)
<tr>
<td>{{ $event->categories->category }}</td>
<td>{{ $event->event_name }}</td>
<td>{{ $event->start_date }}</td>
<td>{{ $event->end_date }}</td>
<td>{{ $event->time }} </td>
<td>{{ $event->address }} </td>
<td>{{ $event->notes }} </td>
<th>
<a href="/events/{{$event->id}}/editEventForm" class="btn btn-primary" role="button" aria-pressed="true">Edit</a>
</th>
<th>
<form action="{{route('events.destroy',[$event->id])}}" method="POST">
@method('DELETE')
@csrf
<button class="btn btn-danger">Delete</button>
</form>
</th>
</tr>
@endforeach
</tbody>
</table>
<br />
<hr>
<div class="panel panel-primary">
<div class="panel-heading"></div>
<div class="panel-body">
<br>
{!! $calendar_details->calendar() !!}
{!! $calendar_details->script() !!}
</div>
</div>
@endsectionTripController.php
public function export($type)
{
return Excel::download(new TripsExport, 'itinerary.' . $type);
}路由
//Exporting
Route::get('export', 'TripController@export')->name('export');
Route::get('importExportView', 'TripController@importExportView');
Route::post('import', 'TripController@import')->name('import');发布于 2020-04-06 12:56:45
把这个放在你的路线上:
Route::get('export/{type?}', 'TripController@export')->name('export');然后在你的控制器里:
public function export($type = 'xls')
{
return Excel::download(new TripsExport, 'itinerary.' . $type);
}然后在你的刀片里,你甚至可以用这个:
{{route('export')}}或者这个:
{{route('export', 'xlsx')}}如果您想导出单个Trip 而不是:
Route::get('/{trip}/export/{type?}', 'TripController@export')->name('export');public function export(Trip $trip, $type = 'xls')
{
return Excel::download(new TripsExport($trip), 'itinerary.' . $type);
}class TripsExport implements FromView
{
public $trip;
public function __construct(Trip $trip){
$this->trip = $trip;
}
public function view(): View
{
// eager load everything you need
$this->trip->load('events.category');
return view('trips.edit', [
'trip' => $this->trip
]);
}
}发布于 2020-04-06 12:57:56
在您的路由文件中,您有一个没有任何参数的导出路由,如果您希望该类型是动态的,那么您必须创建一个具有像下面这样的路由参数的路由:
Route::get('export/{type}', 'TripController@export');也可以为导出函数中的类型参数设置默认值。
public function export($type = 'excel') // this is just an example type
{
return Excel::download(new TripsExport, 'itinerary.' . $type);
}这应该能解决你的问题
最好的。
https://stackoverflow.com/questions/61060251
复制相似问题