如何从会话中提取,编辑会话,然后将其重新插入Laravel?

时间:2019-01-14 22:48:53

标签: php laravel laravel-5

我正在尝试创建购物车控制器。我正在使用Laravel的Http Session实现这一目标。但是,我收到此错误:

  

Symfony \组件\调试\异常\ FatalThrowableError(E_ERROR)   不能使用带字符串偏移量的分配运算符

     

以前的例外    -非法的字符串偏移量'0'

这是我当前的导致此错误的代码:

public function addOrUpdate(Request $request) {
    if(!isset($request->productName) && !isset($request->productId))
        return Redirect::back()->withErrors(['Please fill in all the required fields.'])->withInput();

    # Init cart if not yet set
    if(!$request->session()->has('cart'))
        $request->session()->put('cart', []);

    # Pull and delete the old value
    $product = $request->session()->pull("cart.{$request->productId}", "cart.{$request->productId}");

    # If we managed to pull anything, lets increase the quantity
    if(isset($product)) {
        $product['quantity']++;
        $request->session()->push('cart', [$request->productId => $product]);
        var_dump($request->session('cart')); # Confirmation
        return true;
    }

    # Nothing was pulled, lets add it
    $request->session()->push('cart', [$request->productId => [
        'productName'   => $request->productName,
        'quantity'      => 1
    ]]);
    var_dump($request->session('cart')); # Confirmation
    return true;
}

当我通过var_dump($product)条件之前执行isset()时,它给我的值为string(6) "cart.1"

如果我在通过var_dump(session('cart'))条件之前做过isset(),它会给我(带有测试数据)

array(1) { [0]=> array(1) { [1]=> array(2) { ["productName"]=> string(3) "foo" ["quantity"]=> int(1) } } }

在Laravel的文档中,

#检索和删除项目

pull方法将在单个语句中检索并删除会话中的项目:

$value = $request->session()->pull('key', 'default');

我如何获取当前存储在ID中的产品,将其删除,编辑然后重新插入,因为这似乎正在返回我要获取的数组。

更新

public function addOrUpdate(Request $request) {
    if(!isset($request->productName) && !isset($request->productId))
        return Redirect::back()->withErrors(['Please fill in all the required fields.'])->withInput();

    # Init cart if not yet set
    if(!session()->has('cart'))
        session()->put('cart', []);

    $cart = session('cart');

    if(isset($cart[$request->productId])){
        # Pull and delete the old value
        $product = session()->pull("cart.{$request->productId}", "cart.{$request->productId}");

        # If we managed to pull anything, lets increase the quantity
        if(isset($product)) {
            $product['quantity']++;
            session()->push("cart.{$request->productId}", $product);
            echo 'Updated';
            dd(session('cart'));
            return true;
        }

        return false;
    }

    # Nothing was pulled, lets add it
    session()->push("cart.{$request->productId}",  [
        'productName'   => $request->productName,
        'quantity'      => 1
    ]);
    echo 'Added';
    dd(session('cart'));
    return true;
}

这现在似乎添加了正确的信息,但从未更新。我是否需要flash()会议?

1 个答案:

答案 0 :(得分:0)

这里的主要问题是,Laravel中的HTTP会话仅使用一次。为了保持这些值,必须flash()reFlash()购物车。

public function addOrUpdate(Request $request) {
    if(!isset($request->productName) && !isset($request->productId))
        return Redirect::back()->withErrors(['Please fill in all the required fields.'])->withInput();

    # Init cart if not yet set
    if(!session()->has('cart')) {
        session()->put('cart', []);
        session()->flash('cart');
    }

    $cart = session('cart');

    if(isset($cart[$request->productId])){
        # Pull and delete the old value
        $product = session()->pull("cart.{$request->productId}", "cart.{$request->productId}");

        # If we managed to pull anything, lets increase the quantity
        if(isset($product)) {
            $product[0]['quantity']++;
            session()->push("cart.{$request->productId}", $product[0]);
            session()->reFlash('cart');
        }
    } else {

        # Nothing was pulled, lets add it
        session()->push("cart.{$request->productId}",  [
            'productName'   => $request->productName,
            'quantity'      => 1
        ]);
        session()->reFlash('cart');

    }
}

这里唯一的问题是,如果用户在其他任何地方导航,则其他路线不会重新刷新购物车。因此,必须予以实施。

我通过在我的boot()文件的RouteServiceProvider.php方法中添加以下代码行来实现这一点:

if(session()->has('cart'))
    session()->reFlash('cart');