从发票视图中返回项目数据

时间:2019-11-28 12:21:13

标签: laravel

我使用了以下模型:发票,发票产品和下面列出的pro_stock

public function items()
    {
        return $this->hasMany(invoice_product::class);
    }

返回发票产品,然后

public function products(){
    return $this->belongsTo(prodStock::class);
   }

从库存表和发票控制器获取ID

 public function show($id)
    {
        $items = invoice_product::find($id);
        $invoice = invoice::with(['items.products'])
        ->findOrFail($id);
        return view('pages.InvoiceDetails', compact('invoice'));
    }

在我返回的视野中

 @foreach($invoice->items as $item)
                        <tr>
                        <td>{{$no++}}</td>
                            <td class="table-name">{{$item->products->pro_name}}</td>
                            <td class="table-price">{{$item->unit_price}}</td>
                            <td class="table-qty">{{$item->qty}}</td>
                            <td class="table-total text-right">{{$item->total}}</td>
                        </tr>
                    @endforeach

及其后尘

Trying to get property of non-object (View:

使用print_r($ invoice-> toArray())进行一次测试 返回了这些数据,我想用其名称替换pro_stock_id

[items] => Array ( [0] => Array ( [id] => 130 [invoice_id] => 87 [pro_stock_id] => 7 [unit_price] => 200 [qty] => 1 [total] => 200 [created_at] => 2019-11-27 09:32:20 [updated_at] => 2019-11-27 09:32:20 [products] => ) [1] => Array ( [id] => 131 [invoice_id] => 87 [pro_stock_id] => 8 [unit_price] => 300 [qty] => 12 [total] => 3600 [created_at] => 2019-11-27 09:32:20 [updated_at] => 2019-11-27 09:32:20 [products] => ) [2] => Array ( [id] => 132 [invoice_id] => 87 [pro_stock_id] => 4 [unit_price] => 300 [qty] => 1 [total] => 300 [created_at] => 2019-11-27 09:32:20 [updated_at] => 2019-11-27 09:32:20 [products] => ) ) )

1 个答案:

答案 0 :(得分:0)

尝试一下

products

<td class="table-name">{{$item->products->pro_name}}</td>