How to push an array to an existing session in laravel

11,953

I suppose it can be something like:

// set products.name as array
session()->put('products.name', []);


// somewhere later
session()->push('products.name', $name1);

// somewhere else later
session()->push('products.name', $name2);
Share:
11,953
Alen
Author by

Alen

Updated on June 06, 2022

Comments

  • Alen
    Alen almost 2 years

    Prior to my previous question I have come up with another idea to push array of items in a single session

    For instance I have a session session('products')

    Now what I have, are sets of items for instance.

    Name=Item1
    Class=Good
    
    Name=Item2
    Class=Bad
    
    Name=Item3
    Class=Good
    
    Name=Item4
    Class=Bad
    

    I learnt that

    session()->put('products.name', $name);
    session()->put('products.class', $class);
    

    This would simply put items to it but When I try to put another array to session, it just replaces the values,

    Thus I tried to use push() method

    session()->push('products.name', $name);
    session()->push('products.class', $class);
    

    But it shows that [] operator not supported for strings Does anyone have solution to this?

    I am new to laravel and learning!