Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to push an array to an existing session in laravel

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!

like image 368
Alen Avatar asked Dec 05 '22 15:12

Alen


1 Answers

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);
like image 92
u_mulder Avatar answered Jan 07 '23 23:01

u_mulder