我有一个视图,我想有一个下载PDF的链接。
视图
<div class="col-lg-12 col-xs-12">
<div class="box">
<a href="{{ route('pdf',['download'=>'pdf']) }}">Download PDF</a>
<div class="box-header">
<div style="text-align:center"><h3 class="box-title">PEMELIHARAAN DAN PERAWATAN ALAT UJI </h3></div>
<div style="text-align:center">Jln. Kabupaten Sragen</div>
</div>
<p> Laporan : {{ $pemeliharaan->status }} </p>
<p> Tanggal : {{ $pemeliharaan->created_at }} </p>
<p> Jenis Alat : {{ $pemeliharaan->alat->nama_alat }} </p>
<p> User : {{ $pemeliharaan->user->name }} </p>
<div class="box">
<div class="box-header">
<h3 class="box-title"></h3>
</div>
<div class="box-body no-padding">
<table class="table table-condensed">
<tbody>
<tr>
<th style="width: 10px">#</th>
<th>Pertanyaan</th>
<th>Hasil</th>
</tr>
<tr>
<td>1.</td>
<td>{{ $pemeliharaan->pertanyaan['question1'] }}</td>
<td>{{ $pemeliharaan->pertanyaan['answer1'] }}</td>
</tr>
<tr>
<td>2.</td>
<td>{{ $pemeliharaan->pertanyaan['question2'] }}</td>
<td>{{ $pemeliharaan->pertanyaan['answer2'] }}</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
链接“下载”有错误。
只显示404
调节器
public function showQuestion(Request $request, $id)
{
$pemeliharaan = Pemeliharaan::findOrFail($id);
$pemeliharaan->pertanyaan = json_decode($pemeliharaan->pertanyaan, true);
if ($request->has('download')) {
$pdf = PDF::loadView('users.view_question', $pemeliharaan);
return $pdf->download('view_question.pdf');
}
return view('users.view_question', compact('pemeliharaan'));
}
路线
Route::get('/user/show/question/pdf/{id}','userController@showQuestion')->name('pdf');
Route::get('user/show/question/{id}', 'userController@showQuestion')->name('usershowQuestion');
有人可以帮我下载代码吗?
您没有将id route参数提供给命名路由'pdf'。这导致线$pemeliharaan = Pemeliharaan::find($id)
产生null
,后来出现错误。
试试这个:
{{ route('pdf', [ $id ] }}
为了使它工作,您必须向视图提供变量$id
。你可以这样做
return view('your-view')->with([
"id" => $id
]);