背景
所以我運行這樣的代碼M_Site::get(),它會產生這樣的輸出值:
"MSite": [
{
"id": 1,
"name": "Sudirman",
"created_at": "2022-09-25T23:09:47.000000Z",
"updated_at": "2022-09-25T23:09:47.000000Z",
"created_by": 1,
"updated_by": null,
"deleted_at": null
},
{
"id": 2,
"name": "Cibinong",
"created_at": "2022-09-25T23:09:56.000000Z",
"updated_at": "2022-09-25T23:09:56.000000Z",
"created_by": 1,
"updated_by": null,
"deleted_at": null
},
{
"id": 3,
"name": "Tanah Merdeka",
"created_at": "2022-09-25T23:10:08.000000Z",
"updated_at": "2022-09-25T23:10:08.000000Z",
"created_by": 1,
"updated_by": null,
"deleted_at": null
}
]
增加價值
我只想在開頭添加一個額外的前綴值,如下所示:
[
"id" => 0,
"name" => "All Branch",
"created_at" => "0000-00-25T23:00:00.000000Z",
"updated_at" => "0000-00-25T23:00:00.000000Z",
"created_by" => 1,
"updated_by" => null,
"deleted_at" => null,
]
試試 1
我已經嘗試過添加值的方法,例如向常規陣列添加值,如下所示:
$m_site = [
[
"id" => 0,
"name" => "All Branch",
"created_at" => "0000-00-25T23:00:00.000000Z",
"updated_at" => "0000-00-25T23:00:00.000000Z",
"created_by" => 1,
"updated_by" => null,
"deleted_at" => null,
],
M_Site::get()
];
但是失敗了,結果是這樣的:
"MSite": [
{
"id": 0,
"name": "All Branch",
"created_at": "0000-00-25T23:00:00.000000Z",
"updated_at": "0000-00-25T23:00:00.000000Z",
"created_by": 1,
"updated_by": null,
"deleted_at": null
},
[
{
"id": 1,
"name": "Sudirman",
"created_at": "2022-09-25T23:09:47.000000Z",
"updated_at": "2022-09-25T23:09:47.000000Z",
"created_by": 1,
"updated_by": null,
"deleted_at": null
},
{
"id": 2,
"name": "Cibinong",
"created_at": "2022-09-25T23:09:56.000000Z",
"updated_at": "2022-09-25T23:09:56.000000Z",
"created_by": 1,
"updated_by": null,
"deleted_at": null
},
{
"id": 3,
"name": "Tanah Merdeka",
"created_at": "2022-09-25T23:10:08.000000Z",
"updated_at": "2022-09-25T23:10:08.000000Z",
"created_by": 1,
"updated_by": null,
"deleted_at": null
}
]
]
試試 2
我也嘗試過使用Laravel 集合push中的函式,如下所示:
$m_site = collect([
"id" => 0,
"name" => "All Branch",
"created_at" => "0000-00-25T23:00:00.000000Z",
"updated_at" => "0000-00-25T23:00:00.000000Z",
"created_by" => 1,
"updated_by" => null,
"deleted_at" => null,
]);
$m_site->push(M_Site::get());
但是失敗了,結果是這樣的:
"MSite": {
"id": 0,
"name": "All Branch",
"created_at": "0000-00-25T23:00:00.000000Z",
"updated_at": "0000-00-25T23:00:00.000000Z",
"created_by": 1,
"updated_by": null,
"deleted_at": null,
"0": [
{
"id": 1,
"name": "Sudirman",
"created_at": "2022-09-25T23:09:47.000000Z",
"updated_at": "2022-09-25T23:09:47.000000Z",
"created_by": 1,
"updated_by": null,
"deleted_at": null
},
{
"id": 2,
"name": "Cibinong",
"created_at": "2022-09-25T23:09:56.000000Z",
"updated_at": "2022-09-25T23:09:56.000000Z",
"created_by": 1,
"updated_by": null,
"deleted_at": null
},
{
"id": 3,
"name": "Tanah Merdeka",
"created_at": "2022-09-25T23:10:08.000000Z",
"updated_at": "2022-09-25T23:10:08.000000Z",
"created_by": 1,
"updated_by": null,
"deleted_at": null
}
]
}
期望
我想要的是這樣的結果:
"MSite":
{
{
"id": 0,
"name": "All Branch",
"created_at": "0000-00-25T23:00:00.000000Z",
"updated_at": "0000-00-25T23:00:00.000000Z",
"created_by": 1,
"updated_by": null,
"deleted_at": null,
},
{
"id": 1,
"name": "Sudirman",
"created_at": "2022-09-25T23:09:47.000000Z",
"updated_at": "2022-09-25T23:09:47.000000Z",
"created_by": 1,
"updated_by": null,
"deleted_at": null
},
{
"id": 2,
"name": "Cibinong",
"created_at": "2022-09-25T23:09:56.000000Z",
"updated_at": "2022-09-25T23:09:56.000000Z",
"created_by": 1,
"updated_by": null,
"deleted_at": null
},
{
"id": 3,
"name": "Tanah Merdeka",
"created_at": "2022-09-25T23:10:08.000000Z",
"updated_at": "2022-09-25T23:10:08.000000Z",
"created_by": 1,
"updated_by": null,
"deleted_at": null
}
}
如何生成它?
uj5u.com熱心網友回復:
prepend()
該
prepend方法將一個專案添加到集合的開頭:
$m_site = M_Site::get()->prepend((object)[
"id" => 0,
"name" => "All Branch",
"created_at" => "0000-00-25T23:00:00.000000Z",
"updated_at" => "0000-00-25T23:00:00.000000Z",
"created_by" => 1,
"updated_by" => null,
"deleted_at" => null,
]);
concat()
該
concat方法將給定的陣列或集合的值附加到另一個集合的末尾:
$m_site = collect([
(object)[
"id" => 0,
"name" => "All Branch",
"created_at" => "0000-00-25T23:00:00.000000Z",
"updated_at" => "0000-00-25T23:00:00.000000Z",
"created_by" => 1,
"updated_by" => null,
"deleted_at" => null,
],
])->concat(M_Site::get());
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/531958.html
