Home > Software design >  Push into session issue in Laravel
Push into session issue in Laravel

Time:06-28

Each item contains a list of addons as checkboxes. Every time I check an addon I want to push addon_id into the addons array. It's pushing an addon_id to session but it's not pushing it properly.

This code adds item to cart (Notice: I am creating an empty addons array):

$cart = session()->get('cart', []);
    
if(isset($cart[$request->item_id])) {
    
  $cart[$request->item_id]['qty']  ;
    
} else {
    
   $cart[$request->item_id] = [
    
      'id' => $request->item_id,
      'category_id' => $request->category_id,
      'price' => $request->item_price,
      'item_name' => $request->item_name,
      'qty' => 1,
      'addons' => [],
    
   ];
    
}

session()->put('cart', $cart);

And this code pushes addons to items:

if(isset($request->item_id)) {
    
   session()->push('cart.addons', $request->addon_id);
    
}
            
$cart = session()->get('cart', []);
                
return response()->json([
    
   'message' => 'Addon added',
   'cart' => $cart,
    
]);

This is how addons are being pushed now:

Array
(
    [59] => Array
        (
            [id] => 59
            [category_id] => 27
            [price] => 3.78
            [item_name] => Simple Sandwich
            [qty] => 1
            [addons] => Array
                (
                )
        )

    [57] => Array
        (
            [id] => 57
            [category_id] => 27
            [price] => 5.99
            [item_name] => Cheese Burger
            [qty] => 1
            [addons] => Array
                (
                )
        )

    [addons] => Array
        (
            [0] => 31
        )

    [addons] => Array
        (
            [0] => 61,
            [1] => 60,
            [2] => 31
        )
)

I need them to be pushed like this:

Array
(
    [59] => Array
        (
            [id] => 59
            [category_id] => 27
            [price] => 3.78
            [item_name] => Simple Sandwich
            [qty] => 1
            [addons] => Array
                (
                  [0] => 31
                )
        )

    [57] => Array
        (
            [id] => 57
            [category_id] => 27
            [price] => 5.99
            [item_name] => Cheese Burger
            [qty] => 1
            [addons] => Array
                (
                  [0] => 61,
                  [1] => 60,
                  [2] => 31
                )
        )
)

I tried to find a solution but nothing seems to be working the way I need it.

Help is very much appreciated.

CodePudding user response:

Initially you are making an associative array where item_id is the key, but when you are appending in addons you are not specifying against which item_id the addons array should be updated, so you will have to change

if(isset($request->item_id)) {
    
   session()->push('cart.addons', $request->addon_id);
    
}

to something like this:

if(isset($request->item_id)) {
   $cart = session()->get('cart');
   if(isset($cart[$request->item_id]) {
   array_push($cart[$request->item_id]['addons'],$request->addon_id);
   session()->put('cart',$cart);
 }
}
  • Related