6

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!

1 Answer 1

19

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);
Sign up to request clarification or add additional context in comments.

1 Comment

Yess this works! I made an if condition that if the products session is null then session()->put('products.name', []);. Now I can push items into it thank you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.