Add extra ‘show’ page with PDF [Laravel9]

huangapple go评论66阅读模式
英文:

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&#39;t get single one record.
Route::get(&#39;/generate_pdf&#39;, [StudentController::class, &#39;generate_pdf&#39;])-&gt;name(&#39;generate_pdf&#39;);


// below code works fine
     Route::resource(&#39;ddd&#39;,  StudentController::class)-&gt;parameters([
            &#39;ddd&#39; =&gt; &#39;student&#39;
        ]);

Controller

public function generate_pdf(Student $student, $id)
   {
    $data = Student::whereId($student-&gt;id)-&gt;first();
    $date = new Carbon();        
    $pdf = PDF::loadView(&#39;myPDF&#39;, compact(&#39;data&#39;));     

    return $pdf-&gt;download(&#39;download.pdf&#39;);
}

   public function show(Student $student)
    {
        return view(&#39;show&#39;, compact(&#39;student&#39;));
    }

mypage.blade.php(When User click this link, PDF download starts.)

    &lt;a href=&quot;{{ route(&#39;generate_pdf&#39;,
   $row-&gt;id)}}&quot;class=&quot;btn btn-primarybtn-sm&quot;&gt;
   print PDF&lt;/a&gt;

pdf.blade.php

I wrote like this same as show.blade.php

   &lt;h1&gt;PDF PRINT &lt;/h1&gt;

   Name :    {{ $student-&gt;student_name }}

   Email:    {{ $student-&gt;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(&#39;generate_pdf/{student}&#39;, [
    StudentController::class,
    &#39;generate_pdf&#39;
])-&gt;name(&#39;generate_pdf&#39;);

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(&#39;myPDF&#39;, compact(&#39;student&#39;));     

    return $pdf-&gt;download(&#39;download.pdf&#39;);
}

Also update the link in the blade file to:

&lt;a href=&quot;{{ route(&#39;generate_pdf&#39;, [&#39;student&#39; =&gt; $row-&gt;id]) }}&quot;
   class=&quot;btn btn-primarybtn-sm&quot;
&gt;Print PDF&lt;/a&gt;

References:

huangapple
  • 本文由 发表于 2023年4月17日 11:44:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/76031586.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定