You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何为Eloquent ORM ::get()结果添加自定义数组元素

背景

执行代码 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 Collection的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
    }
]

解决方案

方法1:使用Collection的prepend方法

prepend方法可直接在集合开头添加单个元素,是最简洁的实现方式:

$customItem = [
    "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 = M_Site::get()->prepend($customItem);

方法2:使用merge合并集合

将自定义元素包装成集合后,与站点集合合并,确保顺序正确:

$customCollection = 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 = $customCollection->merge(M_Site::get());

方法3:数组操作转集合

若习惯数组操作,可先将集合转为数组,插入元素后再转回集合:

$customItem = [
    "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,
];

// 转成数组并在开头插入元素
$siteArray = M_Site::get()->toArray();
array_unshift($siteArray, $customItem);

// 转回Collection(按需使用)
$m_site = collect($siteArray);

错误原因说明

  • 方案1错误:M_Site::get()返回的是Collection对象,直接作为数组元素添加会形成嵌套结构,需先展开或用集合方法合并。
  • 方案2错误:初始collect()传入单个关联数组,生成的是键值对集合而非包含元素的数组集合,push会把整个站点集合作为单个元素加入,导致结构混乱。

内容的提问来源于stack exchange,提问作者Muhammad Faisal

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.12 08:50:43