douqian2334 2016-04-02 07:43
浏览 24
已采纳

将值附加到现有会话Laravel 5

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条回答 默认 最新

  • dpmfur2635 2016-04-02 08:01
    关注

    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');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 正弦信号发生器串并联电路电阻无法保持同步怎么办
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序