I have similar shopping cart. In my project Users and Guests can add products to cart. Users its ok. When guest user added product in cart i save it db with session id. its ok too. But when guest user closed the site i cant delete records from db. How can i detect destroyed sessions?

i have a GuestUserProvider which creat new guest user in db

        $session_id = session()->get( '_token' );
        $guests = GuestUser::all();
        foreach ($guests as $guest){

        }
        if(!empty($guest)){
            if($session_id != $guest['session_id']){
                $created = GuestUser::create([
                    'session_id' => $session_id,
                    'ip'    => $clientIP = \Request::ip()
                ]);
            }
        }else{
            $created = GuestUser::create([
                'session_id' => $session_id,
                'ip'    => $clientIP = \Request::ip()
            ]);
        }

And Add to cart function

    public function gocart(Request $request)
    {
    $session_id = session()->get( '_token' );
    if(Auth::User()){
        $ifs = Cart::where(['product_id' => $request['id'], 'user_id' => 
    Auth::user()->id])->get();
    }else{
        $ifs = Cart::where(['product_id' => $request['id'], 'session_id' 
    => $session_id])->get();
    }
    if(count($ifs) > 0) {
        foreach ($ifs as $if) {}
        $count = $if['quantity'];
        $newCount = $count + 1;
        if(count($ifs) >= 1){

            Cart::where('product_id', $request['id'])->update([
                'quantity' => "$newCount"
            ]);
        }

        }else{
        $products = ProductNew::where('id', $request['id'])->get();

        $images = Pimage::select('image')->where('product_id', 
        $request['id'])->first();
        $image = $images['image'];
        foreach ($products as $product) {
            $main = ProductNew::where('group', $product['group'])- 
         >first();
            $title = $main['title'];
            $price = $product['price'];
            $quantity = 1;
            if(Auth::user()){
                Cart::create([
                    'user_id' =>  Auth::user()->id,
                    'product_id' => $request['id'],
                    'session_id' => $session_id,
                    'quantity' => $quantity,
                    'title' => $title,
                    'price' => $price,
                    'image' => $product['image'],
                    'option' => $product['option'],
                    'ip'    => $clientIP = \Request::ip()
                ]);
            }else{
                Cart::create([
                    'user_id' =>  '0',
                    'product_id' => $request['id'],
                    'session_id' => $session_id,
                    'quantity' => $quantity,
                    'title' => $title,
                    'price' => $price,
                    'image' => $product['image'],
                    'option' => $product['option'],
                    'ip'    => $clientIP = \Request::ip()
                ]);
            }
          }

            }
          toastr()->error('Data has been saved successfully!');
          return back();
             }

0 Answers