英文:
Add extra 'show' page with PDF [Laravel9]
问题
I can help you with the translation of your code. Here's the translated version:
我正在学习web.php关于添加额外的展示页面。
我使用了一个样例CRUD程序。
我尝试制作具有PDF打印功能的展示页面。
PDF生成函数没有问题。我可以下载PDF文件,但无法获取单个记录。
目前我写的代码如下:
web.php
//我可以下载PDF文件,但无法获取单个记录。
Route::get('/generate_pdf', [StudentController::class, 'generate_pdf'])->name('generate_pdf');
//以下代码运行正常
Route::resource('ddd', StudentController::class)->parameters([
'ddd' => 'student'
]);
Controller
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-primary btn-sm">
打印PDF
</a>
pdf.blade.php
我写得与show.blade.php相同
<h1>PDF打印</h1>
姓名:{{ $student->student_name }}
电子邮件:{{ $student->student_email }}
请问是否需要进一步的帮助?
This is the translated code. If you have any specific questions or need further assistance, please let me know.
英文:
I'm studying web.php about Adding one extra show page.
I use one of sample CRUD program.
I'm trying to make show page with PDF print.
PDF generate function is fine. I can download the pdf file.but I can't the single record.
currently I wrote like this.
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'
]);
Controller
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(When User click this link, PDF download starts.)
<a href="{{ route('generate_pdf',
$row->id)}}"class="btn btn-primarybtn-sm">
print PDF</a>
pdf.blade.php
I wrote like this same as show.blade.php
<h1>PDF PRINT </h1>
Name : {{ $student->student_name }}
Email: {{ $student->student_email }}
Could you correct my code please?
答案1
得分: 1
The translated content:
>我想要做的是,当用户点击链接时,会显示一个带有PDF的记录。PDF功能正常,但我无法获取所选的记录。
您需要在路由中传递学生参考。
Route::get('generate_pdf/{student}', [
StudentController::class,
'generate_pdf'
])->name('generate_pdf');
控制器函数中不需要第二个参数
public function generate_pdf(Student $student)
{
$date = new Carbon(); // 如果没有使用,可以删除此行
$pdf = PDF::loadView('myPDF', compact('student'));
return $pdf->download('download.pdf');
}
还要更新blade文件中的链接:
<a href="{{ route('generate_pdf', ['student' => $row->id]) }}"
class="btn btn-primary btn-sm"
打印PDF
参考资料:
英文:
>what I would like to do is When user click link One record show up with PDF. PDF function works fine. but somehow I can't not get selected one record.
You would need to have the student reference passed in the route.
Route::get('generate_pdf/{student}', [
StudentController::class,
'generate_pdf'
])->name('generate_pdf');
There is no need of the second parameter in the controller function
public function generate_pdf(Student $student)
{
$date = new Carbon(); // remove this if not used
$pdf = PDF::loadView('myPDF', compact('student'));
return $pdf->download('download.pdf');
}
Also update the link in the blade file to:
<a href="{{ route('generate_pdf', ['student' => $row->id]) }}"
class="btn btn-primarybtn-sm"
>Print PDF</a>
References:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论