Append value to existing session Laravel 5

2019-09-06 02:40发布

Once in my login I set values of user to session like this..

session()->set("user", $user);

And for get the values of the session from anywhere in my application attach with it to $request $request->_user = $user; like this. So I can get it like this from anywhere

$user = $request->_user;

Thing is I want pass some ID of purchase order at the midle of the application. So can I attach it to existing session or how to create new session and append the ID. With that ID, page redirect to paypal and If user cancel the payment I can catch his canceled payment with that ID. That is why I thought session would help me. After a successful payment it is OK to destroy that session. Help me !!!

1条回答
Luminary・发光体
2楼-- · 2019-09-06 03:07

You can continue to append data to the session while the user is using your application. Assuming you want to store a array of purchase IDs, you can do:

session()->push('item_ids', 'item1');

session()->push('item_ids', 'item3');

This will create an array so you can later save all the IDs that the user attempted to purchase:

$items = session('item_ids');

This will return an array with all the item_ids you've pushed to the array:

[
  'item1',
  'item3'
]

After successful purchase or after you've saved the canceled purchase details, just start a new session or delete the item_ids.

// generate new session
session()->regenerate();

// delete item_ids
session()->forget('item_ids');
查看更多
登录 后发表回答