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?
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: