[英]How to display data from controller to blade in laravel?
我在我想要輸出數據的控制器中有此查詢。 我想顯示總計視圖。 現在,它顯示所有訂單中第一個訂單的總數。 例如,如果第一筆訂單的總金額為$ 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] );
}
當我dd($ 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
您的第二個foreach循環可能是問題所在。 您在HTML表中。 對於每一行 ,您將遍歷orderItems
以使產品,價格等為<td>
。看起來您為該項目的Total字段設置了單個列 。
因此,您可能只能夠寫出一項的總數,因為表上沒有更多的空間可以添加更多的<td>
。 要查看是否是問題所在,您可以嘗試將總計放入一個<td>
,並在兩者之間使用管道進行測試。 這會去的第一個循環內 :
<td>
@foreach($total as $item)
{{$item->total }}
{{ $loop->last?"":" | " }}
@endforeach
</td>
在這種情況下,您可能需要為每個項目的3維總計進行重新架構-可能像上面的測試一樣,或者每個產品的總計總計為一個總和,等等。某種允許一行包含一個<td>
(總計)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.