【问题标题】: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);

【问题讨论】:

  • cart 在会话中的值是多少?

标签: 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方法,只需使用sethasget

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-09-17
    • 2011-08-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-26
    • 1970-01-01
    • 2010-12-12
    相关资源
    最近更新 更多