lundi 3 juillet 2017

Laravel push array to session by index

I want to add array key value to session by index, I have this current session

array:2 [▼
  "product" => array:2 [▼
    0 => array:9 [▼
      "name" => "Bunga 1"
      "id" => "7"
      ...
         ]
       ]
    ]

I want to add array key value to session by index inside foreach like this

if(Session::has('cart.product')){
      $cart = Session::get('cart');
      $products = $cart['product'];

      if($products != null){
        foreach($products as $key => $product){

          Session::push('cart.products[$key]', ['new key' => 'new value']);
          Session::push('cart.products[$key]', ['other key' => 'other new value']);

        }
      }

    }

But What I get is

array:2 [▼
  "product" => array:1 [▼
    0 => array:8 [▼
      "name" => "Bunga 1"
      "id" => "7"
      ...
    ]
  ]
  "products[0]" => array:1 [▼
    0 => array:1 [▼
      "new key" => "new value"
    ]
  ]
]

The result I expected is

array:2 [▼
      "product" => array:2 [▼
        0 => array:9 [▼
          "name" => "Bunga 1"
          "id" => "7"
          ...
          "new key" => "new value"
          "other new key" => "other new value"
             ]
           ]
        ]

Where I'm doing wrong?

Thanks in advance



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire