【发布时间】:2019-01-18 13:20:29
【问题描述】:
控制器:
public function OrderHistory(){
$user_id = Auth::User()->id;
$orders = Order::with('ordersz')->where('user_id',$user_id)->get();
return view('users.order_history')->with(compact('orders'));
}
刀片页面:
<table id="example" class="table table-striped table-bordered" style="width:100%;">
<thead>
<tr>
<th>Order ID</th>
{{--<th>Quantity</th>--}}
<th>Ordered Products</th>
<th> Quantity</th>
<th>Total</th>
<th>Order Date</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
@foreach($orders as $order)
<tr>
<td>{{$order->id}}</td>
<td>
{{--@foreach($userCart as $cart)--}}
{{--{{$cart->product_name}}<br>--}}
{{--@endforeach--}}
@foreach($order->ordersz as $pro)
{{$pro->product_id}}<br>// here im displaying the product_id but i want the product name
@endforeach
</td>
<td>
{{--@foreach($userCart as $cart)--}}
{{--{{$cart->product_name}}<br>--}}
{{--@endforeach--}}
@foreach($order->ordersz as $pro)
{{$pro->quantity}}<br>
@endforeach
</td>
<td> {{$order->total_amount}}</td>
<td> {{$order->created_at}}</td>
<td>View Details</td>
</tr>
@endforeach
</tbody>
</table>
我通过user id 获取订单历史记录,并在orders_product 表 中显示product_id。现在我想显示 products 表中的 product_name,我有 products_id
【问题讨论】:
-
你在模型中设置了关系吗?
-
是的,先生,我有
-
您需要发布更多的数据库架构。请包括有问题的模型和您定义的关系。