phparrayslaravelsessionlaravel-4.2

Laravel 4.2 : keep multidimensional array data in session


In my laravel 4.2 application, i want to store the cart item details in session.

cartId =>[
            0 => [
                productId => x,
                quantity  => y
            ],
            1 => [
                productId => u,
                quantity  => v
            ],
            2 => [
                productId => l,
                quantity  => m
            ]

         ]

I didn't found a way except this

Session::push('user.teams', 'developers');

for storing as array in session. but the same is not applicable here


Solution

  • Looks like you can insert the array directly in the session.

    $someArray = ['name' => 'John Doe', 'username' => 'jdoe'];
    
    Session::put('user', $someArray);
    

    When you want to retrieve its value just need to:

    $user = Session::get('user');
    echo $user['name'] // output: John Doe
    

    Same applicable for multidimensional array...