У меня есть этот запрос в контроллере, который выводит данные, как я хочу. Я хотел бы отобразить общее количество для просмотра.
Прямо сейчас он показывает сумму первого заказа для всех заказов. например, если сумма первого заказа составляет 56 долларов, все заказы будут составлять 56 долларов. Как я могу показать сумму каждого заказа?
Вот так выглядит контроллер
public function viewOrders(User $user)
{
//$seller = Auth::user();
$totals = OrderProduct::select("seller_id", DB::Raw("SUM(Subtotal) AS total"), 'order_id')
->where('seller_id', '=', \Auth::user()->id)
->groupBy('seller_id')
->groupBy('order_id')
->get();
//dd($totals);
$orders = Order::whereHas('orderItems.product', function ($query) {
$query->where('seller_id', '=', \Auth::user()->id);
})->get();
//dd($orders);
return view('orders', ['orders'=> $orders, 'total'=> $totals] );
}
когда я сдал ($ итоги) я получаю
Collection {#278 ▼
#items: array:4 [▼
0 => OrderProduct {#302 ▶}
1 => OrderProduct {#303 ▶}
2 => OrderProduct {#304 ▼
#table: "order_product"
#fillable: array:6 [▶]
#connection: "mysql"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:3 [▶]
#original: array:3 [▼
"seller_id" => 1
"total" => "56"------------------->I would like to show this
"order_id" => 35
]
#changes: []
}
3 => OrderProduct {#305 ▼
#table: "order_product"
#fillable: array:6 [▶
#attributes: array:3 [▶]
#original: array:3 [▼
"seller_id" => 1
"total" => "112"------------------->I would like to show this
"order_id" => 36
]
#changes: []
#casts: []
}
]
}
Клинок
@foreach ($order->orderItems as $item)
@if($item->product->user_id == Auth::user()->id)
<td>{{ $item->product->name }}</td>
<td>{{ $item->product->price }}</td>
@endif
@endforeach
@foreach($total as $item)
<td>Total: {{$item->total }}</td>
@endforeach