我正在研究 web.php 关于添加一个额外的显示页面。 我使用示例 CRUD 程序之一。
我正在尝试用 PDF 打印制作展示页面。 PDF 生成功能很好。我可以下载pdf文件,但我不能下载单个记录。 目前我是这样写的。
web.php
//I can download pdf file but I can't get single one record.
Route::get('/generate_pdf', [StudentController::class, 'generate_pdf'])->name('generate_pdf');
// below code works fine
Route::resource('ddd', StudentController::class)->parameters([
'ddd' => 'student'
]);
控制器
public function generate_pdf(Student $student, $id)
{
$data = Student::whereId($student->id)->first();
$date = new Carbon();
$pdf = PDF::loadView('myPDF', compact('data'));
return $pdf->download('download.pdf');
}
public function show(Student $student)
{
return view('show', compact('student'));
}
mypage.blade.php(当用户点击此链接时,PDF 下载开始。)
<a href="{{ route('generate_pdf',
$row->id)}}"class="btn btn-primarybtn-sm">
print PDF</a>
pdf.blade.php
我写的和 show.blade.php 一样
<h1>PDF PRINT </h1>
Name : {{ $student->student_name }}
Email: {{ $student->student_email }}
你能更正我的代码吗?