gpt4 book ai didi

php - Laravel 在 session 中存储数组

转载 作者:可可西里 更新时间:2023-11-01 12:43:33 25 4
gpt4 key购买 nike

我在 session 中存储数组时遇到问题。我正在制作购物车,但它似乎不起作用。

public function __construct(){

$product = array(1,2,3,4);
Session::push('cart', $product);

}

然后像这样在 View 中检索它。

{{Session::get('cart')}}

但是我一直收到这样的错误。

htmlentities() expects parameter 1 to be string, array given

有关如何创建存储一系列商品的购物车的任何线索和建议。

最佳答案

如果您需要将 session 中的数组用作字符串,则需要像这样使用 Collection:

$product = collect([1,2,3,4]);
Session::push('cart', $product);

当您在 html 中使用 {{Session::get('cart');}} 时,这将使它起作用。请注意 Session::push,因为它会始终在 session 中附加新产品。您应该使用 Session::put 以确保产品将始终更新。

关于php - Laravel 在 session 中存储数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37338526/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com