在Laravelu中生成pdf

时间:2018-01-08 12:46:04

标签: laravel-5 pdf-generation

在Laravel中,我希望生成包含项目和公司数据的pdf,我的关系: Invoice.php

    class Invoice extends Model
    {
       public function company() {
          return $this->belongsTo('App\Company');
        }

        public function items() {
          return $this->belongsToMany('App\Item', 'invoice_items', 'invoice_id', 'item_id')
            ->withPivot('quantity');
      }
}

PdfController.php

 public function pdfviewInvoice(Request $request)
    {
        $invoices = DB::table("invoices")->get();
        view()->share('invoices', $invoices);

        if ($request->has('download')) {

            $company = Company::all();
            $items = Item::all();
            $pdf = PDF::loadView('pdf.pdfviewInvoice', compact('company', 'items'));

            return $pdf->download('pdfviewInvoice.pdf');
        }

        return view('pdfviewInvoice');
    }

pdfviewInvoice.blade.php

  @foreach($invoices as $key => $invoice)
            <tr>
                <td>{{ ++$key }}</td>
                <td>{{ $invoice->number }}</td>
                <td>{{ $invoice->status }}</td>
                <td>{{ $invoice->place_issue }}</td>
                <td>{{ $invoice->date_issue }}</td>
                <td>{{ $invoice->date_payment }}</td>
                <td>{{ $invoice->company->name }}</td>
                <td>
                    <table class="table text-center">
                        <td class="text-center">Item</td>
                        <td class="text-center">Unit</td>
                        <td class="text-center">Price</td>
                        <td class="text-center">VAT</td>
                        <td class="text-center">Quantity</td>
                        @foreach($invoice->items as $item)
                            <tr>
                                <td>{{ $item->name }}</td>
                                <td>{{ $item->unit }}</td>
                                <td>{{ $item->price_net }}</td>
                                <td>{{ $item->vat->value."%"}}</td>
                                <td>{{ $item->pivot->quantity }}</td>
                            </tr>
                        @endforeach

当我想显示时会出现错误 &#34;未定义属性:stdClass :: $ items(查看:C:\ praca_inzynierska-master \ resources \ views \ pdf \ pdfviewInvoice.blade.php)&#34;

我做错了什么?在此先感谢:)

0 个答案:

没有答案