MartinZeltin
5 months ago
535
2
Laravel

How to add a a property to my collection?

Posted 5 months ago by MartinZeltin

I have tried using put() method but it didn't work....

I have this collection and I want to add a new property for user with id 73 but how could I do this?

This is my collection

$collection->where('user_id', 73)->put('age', 35);
dump($collection->toArray());

Array
(
    [0] => Array
        (
            [user_id] => 73
            [score] => 3623
        )

    [1] => Array
        (
            [user_id] => 114
            [score] => 4204
        )

    [2] => Array
        (
            [user_id] => 46
            [score] => 1347.6
        )

    [3] => Array
        (
            [user_id] => 7
            [score] => 3841
        )

)

I wanted the result to look like this..

Array
(
    [0] => Array
        (
            [user_id] => 73
            [score] => 3623
            [age] => 35
        )

    [1] => Array
        (
            [user_id] => 114
            [score] => 4204
        )

    [2] => Array
        (
            [user_id] => 46
            [score] => 1347.6
        )

    [3] => Array
        (
            [user_id] => 7
            [score] => 3841
        )

)

Please sign in or create an account to participate in this conversation.