【问题标题】:laravel session [] operator not supported for strings字符串不支持 laravel session [] 运算符
【发布时间】:2017-07-02 20:08:36
【问题描述】:
我正在尝试使用请求数据在现有会话中附加值,
但我收到[] operator not supported for strings 错误。
请求数据是字符串。
这是完整的代码,
if(Session::has('cart')) {
Session::push('cart',$request->id);
} else Session::set('cart',$request->id);
【问题讨论】:
标签:
php
session
laravel-5
【解决方案1】:
Session::push - 将值推送到数组会话值上。
在购物车中你有字符串而不是数组。
# Remove old `string` value.
Session::forget('cart');
if(Session::has('cart')) {
Session::push('cart', $request->id);
} else {
Session::set('cart', array($request->id));
}
会话中的字符串值不需要使用push方法,只需使用set、has、get