laravel - Laravel 从视图中导出
问题描述
我在刀片视图中添加了一个导出按钮,但是我收到以下错误..
函数 App\Http\Controllers\TripController::export() 的参数太少,通过了 0,预期正好 1
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>
@endsection
TripController.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');
解决方案
把它放在你的路线中:
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
]);
}
}