0
我有這個分頁程序實例返回:Laravel 5.3 - 將項目添加到分頁數據?
$products =
{
"products": 3,
"per_page": 10,
"current_page": 1,
"data": [
{
"id": 4,
"product_type": "electronic",
"product_id": 6
},
{
"id": 6,
"type": "electronic",
"product_id": 5
},
{
"id": 9,
"type": "kitchen",
"product_id": 4
}
]
}
我試圖將項目作爲第一項添加到data
關鍵是這樣的:
$firstItem = {
"item": "should be first in data",
"code": 12345
}
最終的結果應該是這樣的:
$products =
{
"products": 3,
"per_page": 10,
"current_page": 1,
"data": [
{ <-
"item": "should be first in data", <-
"code": 12345 <-
}, <-
{
"id": 4,
"product_type": "electronic",
"product_id": 6
},
{
"id": 6,
"type": "electronic",
"product_id": 5
},
{
"id": 9,
"type": "kitchen",
"product_id": 4
}
]
}
我已經嘗試了很多方法,但都沒有得到結果。 任何想法如何完成最終結果?
你有沒有嘗試一旦你分頁數據'array_unshift($ products ['data'],$ firstItem)'? – jfadich
是的,這給了我'Illuminate \\ Pagination \\ LengthAwarePaginator超負荷元素的間接修改' – Wonka
您需要從'LengthAwarePaginator'實例獲取數據作爲數組。嘗試做'$ products = $ products-> toArray()'然後執行我最初發布的代碼。 – jfadich