如何在 Laravel 5.4 中存储数组 cookie? [英] How to store array cookie in laravel 5.4?

查看:41
本文介绍了如何在 Laravel 5.4 中存储数组 cookie?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 laravel 5.4 中有这个功能,但我得到错误.

I have this function in laravel 5.4 but I get error.

$cart[$product->id] = $quantity;
var_dump($cart);
return redirect('catalogs')->withCookie(cookie()->forever('cart', $cart));

var_dump($cart) 包含:

var_dump($cart) contains this:

array(1) { [1]=> string(1) "1" }

错误警告:

Method Symfony\Component\HttpFoundation\Cookie::__toString() must not throw an exception, caught ErrorException: Array to string conversion

如果我只传递字符串值(不是数组),它就成功了.Laravel中是否有办法存储数组cookie?

If I passed just string value (not array), it success. If there any way to store array cookie in Laravel?

谢谢.

推荐答案

cookie 中只能存储字符串.所以试试这个:

Only strings can be stored in a cookie. So try this:

$cart[$product->id] = $quantity;
var_dump($cart);
return redirect('catalogs')->withCookie(cookie()->forever('cart', serialize($cart)));

这篇关于如何在 Laravel 5.4 中存储数组 cookie?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆