如何将数组推送到 Laravel 中的现有会话

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42680059/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 15:30:44  来源:igfitidea点击:

How to push an array to an existing session in laravel

phplaravelsessionlaravel-5laravel-5.3

提问by Alen

Prior to my previous question I have come up with another idea to push array of items in a single session

在我之前的问题之前,我提出了在单个会话中推送项目数组的另一个想法

For instance I have a session session('products')

例如我有一个会话 session('products')

Now what I have, are sets of items for instance.

现在我拥有的是例如物品集。

Name=Item1
Class=Good

Name=Item2
Class=Bad

Name=Item3
Class=Good

Name=Item4
Class=Bad

I learnt that

我了解到

session()->put('products.name', $name);
session()->put('products.class', $class);

This would simply put items to it but When I try to put another array to session, it just replaces the values,

这只会将项目放入其中,但是当我尝试将另一个数组放入会话时,它只会替换值,

Thus I tried to use push()method

因此我尝试使用push()方法

session()->push('products.name', $name);
session()->push('products.class', $class);

But it shows that [] operator not supported for stringsDoes anyone have solution to this?

但它表明[] operator not supported for strings有人有解决方案吗?

I am new to laravel and learning!

我是 Laravel 和学习的新手!

回答by u_mulder

I suppose it can be something like:

我想它可能是这样的:

// set products.name as array
session()->put('products.name', []);


// somewhere later
session()->push('products.name', $name1);

// somewhere else later
session()->push('products.name', $name2);