简体   繁体   English

将新值保存到Laravel会话数组

[英]Save new value to Laravel session array

I have the following code where I loop through the items in the session array and change the value. 我有以下代码,在其中循环遍历会话数组中的项目并更改值。 How can I save it back to the session? 如何将其保存回会话?

foreach(Session::get('cart.program') as &$item) {
    if ($item['id'] == '1xxx') { 
        item['id'] = '2xxx';
        break;
    }
}

One way of doing it 一种方法

$cart = Session::get('cart.program');

foreach($cart as &$item) {
    if ($item['id'] == '1xx') { 
        $item['id'] = '2xx';
        break;
    }
}

Session::put('cart.program', $cart);

Use Session::put() to save to the session in Laravel: 使用Session::put() 保存到 Laravel中的会话

foreach(Session::get('cart.program') as $item){
    if ($item['id'] == '1xxx') { 
        Session::put('cart.program.id', '2xxx');
        break;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM