Laravel 5.1是否可以在我的控制器中循环视图?

问题描述 投票:0回答:1

我使用laravel / dom-pdf生成发票。问题是我有500个帐户要生成,我设法将数据传递给我的视图并循环它。但我在保存pdf时遇到问题。因为保存的pdf代码在我的控制器中..

TestpdfController

$pdf = PDF::loadView('invoices.viewinvoicepdf');
$pdf->save(path/to/save'/'.$inv->accountid.'.pdf'); // problem here is that i cannot grab the accountid for each user and only the last account will be created.

我的第二个选项是内置到foreach,但我认为它效率不高,因为它因为foreach而将视图呈现500次。

foreach($invoices as $inv){
  $pdf = PDF::loadView('invoices.viewinvoicepdf',compact('invoices'));
    $pdf->save(path/to/save'/'.$inv->accountid.'.pdf');
}

如何解决这个问题?

php pdf laravel-5.1
1个回答
0
投票

如果您需要单独保存每个PDF文件,我认为您不能选择使用循环。我假设你的视图文件中有一个循环。您可以在控制器中移动该循环,并让您的视图文件只接受一个invoice

© www.soinside.com 2019 - 2024. All rights reserved.