1

I am trying to store an array of values in a session variable.

So when I am trying to add a variable I simply do:

$request->session()->push('some.array', $id);

When I am trying to pull it out, I do:

$request->session()->pull('some.array', $id);

I can add as many number of elements using the push method but when I try to pull it, it deletes the whole array. How to go around this problem. How to delete only the element with a specific id?

1 Answer 1

2

If you look closely at the docs you will see that

$request->session()->push('some.array', $id);

if actually creating a sub array on session called [array] so when you pull using

$request->session()->pull('some.array');

you are in fact instructing laravel to delete the whole sub array.

So to just delete just a single member of some.array you will have to

print_r( $request->session()->all() );

$tarray = $request->session()->pull('some.array');
unset( $tarray['an_item'] );
$request->session()->push('some.array', $tarray);

print_r( $request->session()->all() );
Sign up to request clarification or add additional context in comments.

5 Comments

But that will only get the item, right? I want to remove the particular item.
I have changed my answer
I think this should work. I thought there will be a method in the codebase to do it already. I will try this and get back to you.
Yea I cannot say that Laravels session management is particularly elegant in this area
I almost forgot to accept this answer. Thank you for the help. :)

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.