[英]How to display data from controller to blade in laravel?
I have this query in controller that output data as i want. 我在我想要输出数据的控制器中有此查询。 I would like to display total to the view.
我想显示总计视图。 Right now it shows total of the first order to all orders.
现在,它显示所有订单中第一个订单的总数。 for example if the first order total is $56, all orders will be $56.
例如,如果第一笔订单的总金额为$ 56,则所有订单的金额均为$ 56。 How can i show total of every order?
如何显示每个订单的总数?
This is how the controller looks like 这就是控制器的外观
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] );
}
when i dd($totals) i get 当我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: []
}
]
}
Blade view 刀片视图
@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
Your second foreach loop is likely the issue. 您的第二个foreach循环可能是问题所在。 You are in an HTML table.
您在HTML表中。 For each row , you are looping through the
orderItems
to make <td>
s for the product, price, etc. It looks like you have a single column set up for the Total field for that item. 对于每一行 ,您将遍历
orderItems
以使产品,价格等为<td>
。看起来您为该项目的Total字段设置了单个列 。
So, you are likely only able to write one item's total, since there is no more room on the table to add more <td>
s. 因此,您可能只能够写出一项的总数,因为表上没有更多的空间可以添加更多的
<td>
。 To see if this is the issue, you can perhaps attempt to put the totals into one <td>
with a pipe in between as a test. 要查看是否是问题所在,您可以尝试将总计放入一个
<td>
,并在两者之间使用管道进行测试。 This would go inside the first loop: 这会去的第一个循环内 :
<td>
@foreach($total as $item)
{{$item->total }}
{{ $loop->last?"":" | " }}
@endforeach
</td>
If this is the case, you may want to re-architect for the 3 dimensional total per item - perhaps like the test above, or perhaps a single sum of the totals per product, etc. Something to allow for one row to contain one <td>
for total(s). 在这种情况下,您可能需要为每个项目的3维总计进行重新架构-可能像上面的测试一样,或者每个产品的总计总计为一个总和,等等。某种允许一行包含一个
<td>
(总计)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.