Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

abdallahsawaqi's avatar

how to update value in session array

i am trying to update a specific value in the session that has array collection but it doesn't seem to work

here is the array

Illuminate\Support\Collection {#596 ▼
  #items: array:6 [▼
    0 => array:4 [▼
      "id" => "jj"
      "name" => "k"
      "date" => "ghj"
      "test" => array:1 [▶]
    ]
    1 => array:3 [▶]
    2 => array:3 [▶]
    3 => array:3 [▶]
    4 => array:3 [▶]
    5 => array:3 [▶]
  ]
  #escapeWhenCastingToString: false
}

and this is my code

foreach (session()->get('key')as $keys => $values) {

        if (session()->get('key')[$keys]["id"] == 'jj') {

            session()->get('key')->map(function ($object, $keys) {

                // Add the new property
                $object[$keys]["id"] = 'works';

                // Return the new object
                return $object;

            });
        }
      }
0 likes
2 replies

Please or to participate in this conversation.