[英]Getting undefined variable in Laravel 5 when passing variable from controller to view
[英]Laravel 5 - Getting values from collection and passing to view
我正在Laravel 5.3中構建訂單管理系統。 我將訂單存儲在兩個不同的表中-order,order_content。 第一個包含訂單的主要數據,另一個包含內容。 我正在使用Crinsane的Shopping Cart軟件包來管理訂單內容。 訂單內容表字段:標識符,實例,內容。 內容字段包含具有特定訂單內容的集合,該集合已進行了序列化。
我的問題是-我需要在特定日期獲取所有訂單內容(產品名稱,代碼,數量)。
這是我走了多遠:
public function showByDate()
{
$datetime_today = new DateTime('now');
$date = $datetime_today->format('Y-m-d');
//get all orders by date, which are confirmed
$orders = Pasutijums::where('pasutijums_uz', '=', $date)
->where('ir_apstiprinats', '=', 1)->get();
foreach($orders as $order)
{
// get order's content
$order_contents = DB::table('order_content')->where('instance', '=', $order->pasut_num)->get();
foreach($order_contents as $order)
{
// unserialize the content field
$collection = unserialize($order->content);
}
}
$items = $collection->all();
return view('admin.kopsavilkumi.pec-datuma')
->with('orders', $orders)
->with('date', $date)
->with('items', $items)
;
}
在視圖中,我嘗試遍歷訂單內容,但僅顯示一種產品...
@foreach($items as $order)
<tr>
<td class="text-primary"> {{ $order->options->code }} </td>
<td> {{ $order->name }} </td>
<td> {{ $order->qty }} </td>
</tr>
@endforeach
我需要反序列化每個字段,將其保存在集合中並傳遞給視圖,以便我可以遍歷它們並在表中顯示它們。
更新:Walter Cejas答案對我有用,但是現在我需要在我的視圖中訪問集合的內容...這是在控制器中使用dd($ collection)時得到的結果:
Collection {#292 ▼
#items: array:2 [▼
0 => Collection {#307 ▼
#items: array:2 [▼
"da951a56dc871db7b48a41f7d14b007b" => CartItem {#308 ▼
+rowId: "da951a56dc871db7b48a41f7d14b007b"
+id: "22"
+qty: "1"
+name: "Preces nosaukums 1"
+price: 7.5
+options: CartItemOptions {#309 ▶}
-associatedModel: null
-taxRate: 0
+"priceTax": 7.5
}
"138ba08bdbf1ac8f17e9e6f257af1b88" => CartItem {#310 ▼
+rowId: "138ba08bdbf1ac8f17e9e6f257af1b88"
+id: "123"
+qty: "1"
+name: "Preces nosaukums 2"
+price: 1.8
+options: CartItemOptions {#311 ▶}
-associatedModel: null
-taxRate: 0
}
]
}
1 => Collection {#306 ▼
#items: array:1 [▼
"da951a56dc871db7b48a41f7d14b007b" => CartItem {#312 ▼
+rowId: "da951a56dc871db7b48a41f7d14b007b"
+id: "22"
+qty: "1"
+name: "Preces nosaukums 3"
+price: 7.5
+options: CartItemOptions {#314 ▶}
-associatedModel: null
-taxRate: 0
}
]
}
]
}
那是因為您用迭代的最后一項覆蓋了集合
$datetime_today = new DateTime('now');
$date = $datetime_today->format('Y-m-d');
//get all orders by date, which are confirmed
$orders = Pasutijums::where('pasutijums_uz', '=', $date)
->where('ir_apstiprinats', '=', 1)->get();
$collection = collect();
foreach($orders as $order)
{
// get order's content
$order_contents = DB::table('order_content')->where('instance', '=', $order-
>pasut_num)->get();
foreach($order_contents as $content)
{
// unserialize the content field
$collection->push(unserialize($content));
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.