Tuesday, February 28, 2017

How to add additional (multiple) values to session without overwriting the existing array?

When I try to add additional data to an session array it overwrites the old ones. Is there a way to add multiple values to session array without overwriting the old one or have I to use push one by one?

Here for example:

session()->put([
  'parent'=>[
     'name'=>'jhon', 
     'surname'=>'doe', 
  ] 
]); 

Now with session()->all() I get:

[
   "parent" => [
      "name" => "jhon",
      "surname" => "doe",
   ],
]

When I want to add additional values with put for example:

session()->put([
  'parent'=>[
     'gender'=>'male', 
     'phone'=>'000000', 
  ] 
]); 

No I get this with session()->all():

[
   "parent" => [
     "gender" => "male", 
     "phone" => "000000",
   ],
]

But I want:

[
   "parent" => [
      "name" => "jhon",
      "surname" => "doe",
      "gender" => "male", 
      "phone" => "000000",
   ],
]

So how can I add additional (multiple) data to an existing session array without touching the old ones?




via Skeletor

Advertisement