首页 > 解决方案 > 如何通过压缩发送收藏到pdf功能

问题描述

我正在尝试为每个紧凑型发送一个数组,但是在查看它时会引发错误。

销售控制器.php

public function generarRecibo($id)
{
    $sales = Sale::with('client', 'products')->where('id', '=', $id)->get();

    $pdf = PDF::loadView('pdf.sales-recibo', compact(['sales']) );

    return $pdf->stream('Recibo N° '.$id.'.pdf');
}

在此处输入图像描述

销售-recibo.blade.php

<table class="table">
<tbody>
    
    @foreach ($sales->products as $product)
        <tr>
            <td style="border: 0px;" colspan="3">{{ $product->name }}</td>
        </tr>
        
        <tr>
            <td style="border: 0px;">
                <ul>
                    <li>{{ $product->quantity }} X ${{ number_format($product->price, 0,',','.') }}</li>
                </ul>
            </td>
            <td style="border: 0px;" width="150px"></td>
            <td style="border: 0px;" width="150px">${{ number_format($product->price, 0,',','.') }}</td>
            
        </tr>
    @endforeach
</tbody>
</table>

标签: phplaravel

解决方案


代替get()使用find()方法

 $sales = Sale::with('client', 'products')->find($id);

你也有紧凑的问题

 $pdf = PDF::loadView('pdf.sales-recibo', compact('sales') );

推荐阅读