我想通过id从表导出到excel。
就像这样:
use Maatwebsite\Excel\Facades\Excel;
use App\Exports\JobExport;
public function export($id)
{
$query=DB::table('applyeds')
->where('job_id',$id)
->get();
return Excel::download( $query, 'job.xlsx');
}刀片:<a href="{{url('job/export',$job->id)}}" class="button big ripple-effect">Export to Excel</a>
路线:Route::get('job/export/{id}', 'JobsController@export');
给我空的Excel。
发布于 2019-07-19 09:43:14
您没有使用您的JobExport导出。您不能将数据传递给JobExport
首先,使用命令进行导出:
php artisan make:export JobExport您的JobExport必须具有类似于以下内容的数据:
<?php
namespace App\Exports;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
use Illuminate\Contracts\View\View;
use Maatwebsite\Excel\Concerns\FromView;
use Maatwebsite\Excel\Concerns\Exportable;
class JobExport implements FromView,ShouldAutoSize
{
use Exportable;
private $errors = [];
public function __construct($error_list){
$this->errors = $error_list;
}
public function view(): View
{
return view('exports.job_export_sheet', [
'errors' => $this->errors,
]);
}
}可以使用以下代码将数据发送到导出:
return Excel::download(new (new JobExport($query)), 'users.xlsx');之后,在资源/视图/导出/作业_导出_工作表中生成一个视图
在job_export_sheet.blade.php中添加以下代码
<table>
<thead>
<tr>
<th>Row No.</th>
<th>Sheet Name</th>
<th>Row Name</th>
<th>List Of Errors</th>
</tr>
</thead>
<tbody>
@foreach($errors as $key=>$error)
<tr>
<td>{{ $error[0]}}</td>
<td>{{ $error[1]}}</td>
<td>{{ $error[2]}}</td>
<td>{{ $error[3]}}</td>
</tr>
@endforeach
</tbody>
将下载名为users.xlsx的excelsheet。发送$query的方式是错误的。
https://stackoverflow.com/questions/57098420
复制相似问题