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

查看:203
本文介绍了如何在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天全站免登陆