我在会话中存储数组时遇到问题.我正在制作购物车,但它似乎不起作用.
I have been having trouble storing an array in session. I am making a shopping cart and it doesn't seem to work.
public function __construct(){
$product = array(1,2,3,4);
Session::push('cart', $product);
}
然后像这样在视图中检索它.
and then retrieve it in the view like this.
{{Session::get('cart')}}
但是我一直收到这样的错误.
However I keep getting an error like this.
htmlentities() expects parameter 1 to be string, array given
有关如何创建可存储一系列商品的购物车的任何线索和建议.
Any clues and advice on how to create a shopping cart that stores an array of items.
如果你需要使用 session 中的数组作为字符串,你需要像这样使用 Collection:
If you need to use the array from session as a string, you need to use Collection like this:
$product = collect([1,2,3,4]);
Session::push('cart', $product);
当您在 html 中使用 {{Session::get('cart');}}
时,这将使它起作用.请注意 Session::push
因为它会在会话中始终附加新产品.您应该使用 Session::put
以确保产品将始终更新.
This will make it work when you will be using {{Session::get('cart');}}
in your htmls. Be aware of Session::push
because it will append always the new products in sessions. You should be using Session::put
to be sure the products will be always updating.
这篇关于Laravel 在会话中存储数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!