使用Laravel生成PDF

时间:2019-02-20 19:51:14

标签: laravel

我正在尝试生成pdf格式的报告,我有一个foreach循环,该循环遍历对象,由于某种原因,数据未显示在下载的pdf上。  任何帮助表示赞赏 这是我的查看页面:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title></title>
  </head>
  <body>
<table class="table table-striped">
    <tr>
        <th>Row No.</th>
        <th>Entry ID</th>
        <th>DC</th>
        <th>Customer Name</th>
        <th>ID No.</th>
        <th>Referance No.</th>
        <th>Entry Purpose</th>
        <th>Time In</th>
        <th>Time Out</th>
        <th>Card No.</th>
        <th>Cabinets</th>
        <th>APC Card</th>
        <th>Key</th>
    </tr>
    @foreach ($assignees as $assignee)
    <tr>
        <td>{{ ++$i }}</td>
        <td>{{ $assignee->id }}</td>
        <td>{{ $assignee->datacenter }}</td>
        <td>{{ $assignee->cust->idnumber }}</td>
        <td>{{ $assignee->custidno}}</td>
        <td>{{ $assignee->refnumber }}</td>
        <td>{{ $assignee->entrypurpose}}</td>
        <td>{{ $assignee->timein }}</td>
        <td>{{ $assignee->timeout }}</td>
        <td>{{ $assignee->cardno }}</td>
        <td>{{ $assignee->cabinet }}</td>
        <td>{{ $assignee->apccard }}</td>
        <td>{{ $assignee->key }}</td>
      </tr>
    @endforeach

</table>
<div class="pull-right">
<strong> Report Generated on {{ date("d.m.Y") }} at {{date("h:i:sa")}}  </strong>
</body>
</html>

这是我的控制器:

public function downloadPDF($id){
      $assignees = assignee::latest();

      $pdf = PDF::loadView('assignees.pdf', compact('assignees'));
      return $pdf->download('invoice.pdf');

    }

2 个答案:

答案 0 :(得分:0)

代替:

var plusOrMinus = _.shuffle([-1, 1])[0];

要做:

 $assignees = assignee::latest();

由于 $assignees = assignee::latest()->get() 返回的是latest()对象而不是集合,因此您没有任何需要循环的地方。您需要调用QueryBuilder来执行查询并返回结果。

此外,略微挑剔:根据Laravel遵循的PSR-2,类(受让人)应该是cammel案,以大写字母开头。

答案 1 :(得分:0)

    var pupils = [
    { id: 0, name: 'will' },
    { id: 1, name: 'megan' }
];

function findPupil(property, value, find) {
    var find_value = '';
    for (var i = 0; i < pupils.length; i++) {
        if (find_value == '') {
            Object.keys(pupils[i]).forEach(function (key) {
                if (key == property && pupils[i][key] == value) {
                    find_value = pupils[i][find];
                }
            });
        } else {
            break;
        }
    }
    console.log(find_value);
    return find_value;
}

var name = findPupil('id', 1, 'name');

将刀片更新为此并检查:

  • 在末尾的<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> </head> <body> @if(count($assignees) > 0) <table class="table table-striped"> <tr> <th>Row No.</th> <th>Entry ID</th> <th>DC</th> <th>Customer Name</th> <th>ID No.</th> <th>Referance No.</th> <th>Entry Purpose</th> <th>Time In</th> <th>Time Out</th> <th>Card No.</th> <th>Cabinets</th> <th>APC Card</th> <th>Key</th> </tr> @foreach ($assignees as $assignee) <tr> <td>{{ $loop->index + 1 }}</td> <td>{{ $assignee->id }}</td> <td>{{ $assignee->datacenter }}</td> <td>{{ $assignee->cust->idnumber }}</td> <td>{{ $assignee->custidno}}</td> <td>{{ $assignee->refnumber }}</td> <td>{{ $assignee->entrypurpose}}</td> <td>{{ $assignee->timein }}</td> <td>{{ $assignee->timeout }}</td> <td>{{ $assignee->cardno }}</td> <td>{{ $assignee->cabinet }}</td> <td>{{ $assignee->apccard }}</td> <td>{{ $assignee->key }}</td> </tr> @endforeach </table> @else <p>No data found!</p> @endif <div class="pull-right"> <strong> Report Generated on {{ date("d.m.Y") }} at {{date("h:i:sa")}} </strong> </div> </body> </html> 标记后缺少结束</div>标记
  • $ i已更改为strong
  • 我添加了一个条件来检查数据是否存在,否则显示$loop->index