【发布时间】:2018-08-22 11:56:41
【问题描述】:
你能再帮帮我吗?我有 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。我只是不知道如何在模型和控制器上做到这一点。请帮助
【问题讨论】:
-
您在
orders表中没有item_id字段。也许您必须将belongsTO更改为belongsTo -
您不能使用
belongsTo,因为它属于多个,我可以看到您有一个product_quantity 表。 -
@MaruAmallo 抱歉,我忘了编辑,product_id 是 item_id。 belongsTo 不是主要问题,因为它现在获取数据,我想要的是使用 Order Table 到 Product 表的 quantity_id 的项目名称。
-
我将详细信息数量_id 获取到订单表并获取 item_id(产品 id) 现在我不知道如何构建模型和控制器以在我的视图中显示产品名称
标签: php laravel laravel-5.4 laravel-eloquent