简体   繁体   English

如何将新数组添加到 Laravel 中的全局会话数组?

[英]how to add new array to an global session array in laravel?

I want to add new arrays to an global session array in laravel 5.6我想在 laravel 5.6 的全局会话数组中添加新数组

The global session is cart .全局会话是cart I want add items to this array.我想向这个数组添加项目。

I try this:我试试这个:

for first time:第一次:

$item = ['key' => 'val1'];

session()->push('cart', $item);
dd(session()->get('cart'));

It works:有用:

array:1 [▼
  0 => array:1 [▼
    "key" => "val1"
  ]
]

Now, I change $item = ['key' => 'val1'];现在,我更改$item = ['key' => 'val1']; to $item = ['key' => 'val2'];$item = ['key' => 'val2']; and refresh the page again.并再次刷新页面。

but it remove "key" => "val1" and return this:但它删除了"key" => "val1"并返回:

array:1 [▼
      0 => array:1 [▼
        "key" => "val2"
      ]
    ]

what's my wrong?我怎么了?

So, everything is correct here.所以,这里一切都是正确的。 First you add val1 under key .首先在key下添加val1 It is stored on SESSION.它存储在 SESSION 上。 Next you replace val1 with val2 .接下来,您val1替换val2 You can either add val2 under key2 , or use dot notation:您可以在key2下添加val2 ,或使用点表示法:

session()->push('cart.key', `val1`);
session()->push('cart.key', `val2`);
dd(session()->get('cart'));

for more details https://laravel.com/docs/7.x/helpers#method-session更多详情https://laravel.com/docs/7.x/helpers#method-session

session()->get('key');

session()->put('key', $value);

session(['chairs' => 7, 'instruments' => 3]);

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

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