Может ли кто-нибудь помочь мне отобразить название продукта? У меня есть продукт, хранящийся в моей записи о покупке, и теперь я хочу отобразить имя продукта в записи productquantity.
У меня есть 3 таблицы
PurchaseRecord
Productquantities
Товар
контроллер:
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
доступ к объекту продукта как $Request->productquantity->product->name