[英]Access relation of pivot table in Laravel
I have three models Bill
, Product
and Process
. 我有三种型号Bill
, Product
and Process
。 Bill
has a ManyToMany relationship with Product
and the pivot table have some extra fields. Bill
与Product
有一个ManyToMany关系,数据透视表有一些额外的字段。 I write the Bill
model class like follow: 我写Bill
模型类如下:
<?php
class Bill extends Model
{
function products(){
return $this->belongsToMany(\App\Product::class)
->withPivot('process_id') // the id of the Process
->withTimestamps();
}
}
The Process
model is a simple table with an id
and a name
. Process
模型是一个带有id
和name
的简单表。 I am associating the id in the pivot table for reference the Process, the name could change over time but still referring the same concept so I can't associate the name. 我正在关联数据透视表中的id以供参考Process,名称可能会随着时间的推移而改变,但仍然引用相同的概念,因此我无法关联该名称。
The show page for a single Bill
lists the products associated in a table like follow: 单个Bill
的显示页面列出了表格中关联的产品,如下所示:
@foreach($bill->products as $product)
<tr>
<td>{{$product->barcode}}</td>
<td>{{$product->pivot->process_id}}</td>
</tr>
@endforeach
So the problem is that I need the name of the process, but I have the id. 所以问题是我需要进程的名称,但我有id。 I'm not sure how I could get the name. 我不确定我怎么能得到这个名字。
Thanks 谢谢
Without having a direct relation to Process
you will likely need to add a helper on your Product
model to get the name of Process
. 如果没有与Process
有直接关系,您可能需要在Product
模型上添加一个帮助程序以获取Process
的名称。
In your Product model: 在您的产品型号中:
public function processName($processId) {
return Process::where('id', $processId)->pluck('name')->first();
}
In your view: 在你看来:
<td>{{$product->processName($product->pivot->process_id) }}</td>
There may be a better way, but concept this should work. 可能有更好的方法,但这应该有效。
I think you can use an own Pivot Model, eg ProductBill
in order to achieve this. 我认为您可以使用自己的Pivot模型,例如ProductBill
来实现这一目标。
class ProductBill extends Pivot {
public function process() {
return $this->belongsTo(Process::class);
}
}
By using this model in your products
relation on Bill
通过在Bill
products
关系中使用此模型
class Bill extends Model {
function products() {
return $this->belongsToMany(\App\Product::class)
->withPivot('process_id')
->using(ProductBill::class)
->withTimestamps();
}
}
When accessing $product->pivot
you should get an instance of MyCustomPivotModel
now, hence you should be able to do the following: 访问$product->pivot
您应该立即获得MyCustomPivotModel
的实例,因此您应该能够执行以下操作:
<td>{{$product->pivot->process->name}}</td>
(Unfortunatelly I not able to doublecheck right now :/) (不幸的是,我现在无法进行双重检查:/)
I know it's not the most elegant of solutions. 我知道这不是最优雅的解决方案。 but you could always simply do: 但你可以随时做到:
Process::find($product->pivot->process_id)->name;
I wouldn't advice this though as you are looping through an array already, so the overheads to doing something like this would be quite large. 我不建议这样做,因为你已经循环遍历一个数组,所以做这样的事情的开销会非常大。
Another solution would be to create a Pivot class called say BillProductPivot
which would have a relationship to return both Product
and Process
, then when you call them you should be using eager loading to get the relationships. 另一个解决方案是创建一个名为BillProductPivot
的Pivot类,该类具有返回Product
和Process
的关系,然后当您调用它们时,您应该使用预先加载来获取关系。 end product might look like this: 最终产品可能如下所示:
$bill->load('productPivots', 'productPivot.process', 'productPivot.product');
@foreach($bill->productPivots as $productPivot)
<tr>
<td>{{$productPivot->product->barcode}}</td>
<td>{{$productPivot->process->name}}</td>
</tr>
@endforeach
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.