I am trying to display an order page for users based on two tables: orders and order_product.
The orders table keeps all the useful data for an order, as seen in the model:
Order.php
protected $fillable = [
'user_id', 'billing_fname', 'billing_lname', 'billing_email', 'billing_phone','billing_address',
'billing_county', 'billing_locality', 'billing_zipcode', 'billing_total', 'shipped'
];
public function user(){
return $this->belongsTo('App\User');
}
public function products(){
return $this->belongsToMany('App\Product')->withPivot('quantity');
}
The order_product table displays infos about how many products were bought on that order and what quantity for each product.
OrderProduct.php
protected $table = 'order_product';
protected $fillable = ['order_id', 'product_id', 'quantity'];
Now, the problem is I want to display all these bounded infos in a view but I am getting all the products id's from all the orders not only the specific one:
myorders.blade.php
<ul class="order-list">
@foreach($orders as $order)
<div class="list-group">
<a href="#" class="list-group-item list-group-item-action flex-column align-items-start active">
<div class="d-flex w-100 justify-content-between">
<h5 class="mb-1">Order {{$order->id}}</h5>
<small>
@if(!$o->shipped)
Not shipped.
@else
Shipped.
@endif
</small>
</div>
<p class="mb-1">User: {{$order->billing_fname}} {{$order->billing_lname}} | Date: {{$order->created_at}} | Total: {{$order->billing_total}} $</p>
<small>Adresa: {{$order->billing_county}} {{$order->billing_address}} {{$order->billing_city}}</small>
</a>
@foreach($products as $product)
<a href="#" class="list-group-item list-group-item-action flex-column align-items-start">
<div class="d-flex w-100 justify-content-between">
<h5 class="mb-1">{{$product->name}}</h5>
<small class="text-muted">Cantitate: {{$product->quantity}}</small>
</div>
<p class="mb-1">Price: {{$product->price}}</p>
</a>
@endforeach
</div>
@endforeach
</ul>
In the controller I tried to access all the data in 3 different foreach statements, as follows:
OrderController
public function index()
{
$user_id = Auth::user()->id;
$orders = Order::where('user_id', $user_id)->get(); //from orders table I need all the orders placed by the authenticate user
$items = array();
foreach($orders as $order){
$items[] = OrderProduct::where('order_id', $order->id)->first(); //from order_product (the pivot table) I need all the data from order_id equals the id in orders
}
$products = array();
foreach($items as $item){
$prod = Product::where('id', $item->product_id)->first(); //searching for the products in the products table with the id's from order_product
$products[] = $prod;
}
return view('orders.myorders', array(
'orders' => $orders,
'products' => $products
));
// dd($products);
}
What I am getting is all the products foreach order, so the view looks something like:
First order id:
-all the products found in array
Second order id:
-again all the products found in array
-and so on, while I only need the specific products for that order
Please feel free to ask anything if I wasn't clear enough. I am new in this field and I would appreciate any piece of advice.
Thanks a bunch.