谁能帮我如何显示产品名称?我的购买记录中存储了产品数量,现在我想在产品数量记录中显示产品名称。
我有3张 table
购买记录
- 编号
- 采购数量
- 产品数量id
产品数量
- 编号
- 产品编号
产品
- 编号
- 产品名称
Controller :
public function createpurchase(Request $request)
{
$purchasenumber = $request->purchasenumber;
$dataPurchaseRecord = Purchaserecord::where('purchasenumber','=',$purchasenumber)->with('productquantity')->get();
return view('admin.createpurchase',compact('purchasenumber', 'dataPurchaseRecord'));
}
查看:
@forelse($dataPurchaseRecord as $Request)
<tr>
<td> <a class="name">{{$Request->productquantity->prod_id}}</a></td>
<td><em class="productprice">{{$Request->quantity}}</em> </td>
<td style="width:100px;" class="td-actions"><a href="javascript:;" class="delete-modal btn btn-mini btn-danger" data-id="{{$Request->id}}" ><i class="icon-minus"> </i>Remove</a> </td>
</tr>
@empty
<tr><td colspan='4'><em>No Data</em></td></tr>
@endforelse
型号:
public function product()
{
return $this->belongsTO('App\Product', 'prodquantityid', 'id');
}
{{$Request->productquantity->prod_id}}
应该显示产品名称而不是 id
我如何构建模型并对此进行查看?
最佳答案
您可以将您的模型定义为
Class Purchaserecord extends Model{
public function productquantity()
{
return $this->belongsTo('App\Productquantities', 'prodquantityid');
}
}
Class Productquantities extends Model{
public function product()
{
return $this->belongsTo('App\Product', 'prod_id');
}
public function purchaserecords()
{
return $this->hasMany('App\Productquantities','prod_id');
}
}
Class Product extends Model{
public function productquantities()
{
return $this->hasMany('App\Productquantities','prod_id');
}
}
现在急切加载你的数据
$dataPurchaseRecord = Purchaserecord::where('purchasenumber','=',$purchasenumber)
->with('productquantity.product')
->get();
在循环中,您以 $Request->productquantity->product->name
访问产品对象
关于php - Laravel 3 表关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51067738/