简体   繁体   English

Laravel从数据库获取ID以下载pdf刀片

[英]Laravel getting id from database to download pdf blade

I am using dompdf currently and I encounter this issue where the id returns me pdf instead of the value I want such as 2 or 1 or 3. 我当前使用的是dompdf,遇到此问题时ID会返回pdf而不是我想要的值(例如2或1或3)。

Route: 路线:

Route::get('pdfview/{id}',array('as'=>'pdfview','uses'=>'pdfDownloadController@pdfview'));

I get the id pdf when I do something like this: 当我做这样的事情时,我得到了id pdf:

 public function pdfview($id)
    {
        dd($id); --> return id = pdf
    $object = UserInfo::where('id',$id)->get();
    dd($object); --> return empty collection
}

Collection {#395 ▼
  #items: []
}

I get the id as null when I do like this: 这样做时,我得到的id为null:

    public function pdfview(Request $request)
    {
         $id = UserInfo::find($request->id);
         dd($id);
}

test.blade.php (use this link to download the file) test.blade.php(使用此链接下载文件)

<a href="{{ route('pdfview',['download'=>'pdf']) }}">Download PDF</a>

That is the problem in this tag you have. 那就是你这个标签中的问题。

test.blade.php (use this link to download the file) 'pdf']) }}">Download PDF test.blade.php(使用此链接下载文件)'pdf'])}}“>下载PDF

You are passing the string 'pdf' to the route. 您正在将字符串“ pdf”传递给路由。 thats is the same string it returns back to you. 多数民众赞成与它返回给您相同的字符串。 you are suppose to pass the actual ID inside your VIEW file. 您应该在VIEW文件中传递实际ID。 this way it returns an integer that comes from database. 这样,它将返回来自数据库的整数。

EX: EX:

 <a href="{{url('/pdfview')}}/{{$post->id}}">Download PDF</a>

In that example $post->id is the ID of the $post it coming from database, and thats what you need to pass to that route and to be able to fetch more data from database based on that ID. 在该示例中,$ post-> id是它来自数据库的$ post的ID,这就是您需要传递到该路由并能够基于该ID从数据库中获取更多数据的内容。

Hopefully it make sense now. 希望现在有意义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM