在 Laravel 模型关系中显示产品名称
Show Product Name in Laravel Model Relationship
你能再帮我一下吗?我有 3 张桌子
Product Table
id
product_name
Productquantity Table
id
item_id
price
Orders
id
req_id
quantity_id
quantity
amount
如何在我的 CartView 中获取产品名称?我的代码中只有这个
订购型号
public function product()
{
return $this->belongsTO('App\Productquantity', 'item_id', 'id');
}
我的控制器
$dataReqorder = Reqorder::where('req_id','=', $shoppingId)->with('product')->get();
我的观点
@foreach($dataReqorder as $order)
<tr class="item{{$order->id}}">
<td> <a href="products/{{$order->id}}" class="name">{{$order->product->prod_id}}</a> </td>
<td>{{$order->amount}}</td>
<td>{{$order->quantity}}</td>
</tr>
@endforeach
我希望这个 {{$order->product->prod_id}} 应该是产品名称而不是产品 ID..我只是不知道如何在模型上做控制器.. 请帮忙
我认为你需要重新订购一些东西
// Reorder tables and models
Table Schema and models
Product Model (Table name => 'products')
id
product_name
OrderProduct (Table name => 'order_product')
id
product_id
order_id
price
quantity_id
quantity
Order Model (Table name => 'orders')
id
req_id
amount
// Product Model
public function orders()
{
return $this->belongsToMany('App\Order')->withPivot('price', 'quantity_id','quantity');
}
// Order Model
public function products()
{
return $this->belongsToMany('App\Product')->withPivot('price', 'quantity_id','quantity');
}
// Controller
$dataReqorder = Order::where('req_id','=', $shoppingId)->with('products')->get();
// View
@foreach($dataReqorder as $order)
<tr class="item{{$order->id}}">
@foreach($order->products as $product)
{{$product->id}}<br>
{{$product->pivot->price}}<br>
{{$product->pivot->quantity_id}}<br>
{{$product->pivot->quantity}}<br>
@endforeach
<td>{{$order->amount}}</td>
<td>{{$order->quantity}}</td>
</tr>
@endforeach
有关更多信息,我建议您查看文档
https://laravel.com/docs/5.6/eloquent-relationships#many-to-many
你能再帮我一下吗?我有 3 张桌子
Product Table
id
product_name
Productquantity Table
id
item_id
price
Orders
id
req_id
quantity_id
quantity
amount
如何在我的 CartView 中获取产品名称?我的代码中只有这个
订购型号
public function product()
{
return $this->belongsTO('App\Productquantity', 'item_id', 'id');
}
我的控制器
$dataReqorder = Reqorder::where('req_id','=', $shoppingId)->with('product')->get();
我的观点
@foreach($dataReqorder as $order)
<tr class="item{{$order->id}}">
<td> <a href="products/{{$order->id}}" class="name">{{$order->product->prod_id}}</a> </td>
<td>{{$order->amount}}</td>
<td>{{$order->quantity}}</td>
</tr>
@endforeach
我希望这个 {{$order->product->prod_id}} 应该是产品名称而不是产品 ID..我只是不知道如何在模型上做控制器.. 请帮忙
我认为你需要重新订购一些东西
// Reorder tables and models
Table Schema and models
Product Model (Table name => 'products')
id
product_name
OrderProduct (Table name => 'order_product')
id
product_id
order_id
price
quantity_id
quantity
Order Model (Table name => 'orders')
id
req_id
amount
// Product Model
public function orders()
{
return $this->belongsToMany('App\Order')->withPivot('price', 'quantity_id','quantity');
}
// Order Model
public function products()
{
return $this->belongsToMany('App\Product')->withPivot('price', 'quantity_id','quantity');
}
// Controller
$dataReqorder = Order::where('req_id','=', $shoppingId)->with('products')->get();
// View
@foreach($dataReqorder as $order)
<tr class="item{{$order->id}}">
@foreach($order->products as $product)
{{$product->id}}<br>
{{$product->pivot->price}}<br>
{{$product->pivot->quantity_id}}<br>
{{$product->pivot->quantity}}<br>
@endforeach
<td>{{$order->amount}}</td>
<td>{{$order->quantity}}</td>
</tr>
@endforeach
有关更多信息,我建议您查看文档
https://laravel.com/docs/5.6/eloquent-relationships#many-to-many