【发布时间】:2019-02-19 09:00:39
【问题描述】:
我有三个模型Bill、Product 和Process。 Bill 与 Product 具有多对多关系,并且数据透视表有一些额外的字段。我编写了Bill 模型类,如下所示:
<?php
class Bill extends Model
{
function products(){
return $this->belongsToMany(\App\Product::class)
->withPivot('process_id') // the id of the Process
->withTimestamps();
}
}
Process 模型是一个包含id 和name 的简单表。我正在关联数据透视表中的 id 以引用进程,名称可能会随着时间而改变,但仍然引用相同的概念,因此我无法关联名称。
单个Bill 的显示页面在如下表中列出了相关联的产品:
@foreach($bill->products as $product)
<tr>
<td>{{$product->barcode}}</td>
<td>{{$product->pivot->process_id}}</td>
</tr>
@endforeach
所以问题是我需要进程的名称,但我有 id。我不知道我怎么能得到这个名字。
谢谢
【问题讨论】:
标签: php laravel relationship