Home > database >  How to add array values to Eloquent ORM ::get() output
How to add array values to Eloquent ORM ::get() output

Time:11-13

Background

So I run code like this M_Site::get() and it produces an output value like this:

"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
            }
        ]

Value to add

I just want to add an extra prefix value in the beginning like this:

[
    "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,
]

Try 1

I've tried the way with adding values like adding values to a regular array like this:

$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()
];

But failed, the result is like this:

"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
                }
            ]
        ]

Try 2

I've also tried using the push function from Laravel's Collection like this:

$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());

But failed, the result is like this:

"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
                }
            ]
        } 

Expectations

What I want is a result like this:

"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
            }
        }

How to generate it like that?

CodePudding user response:

prepend()

The prepend method adds an item to the beginning of the collection:

$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()

The concat method appends the given array or collection's values onto the end of another collection:

$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());
  • Related