如何在laravel项目中检测到被破坏的访客会话?

时间:2019-05-28 11:32:51

标签: javascript php laravel

我有类似的购物车。在我的项目中,用户和访客可以将产品添加到购物车。用户还可以。当来宾用户在购物车中添加产品时,我将其保存为带有会话ID的数据库。还可以。但是,当来宾用户关闭站点时,我无法从db中删除记录。我如何检测被破坏的会话?

我有一个GuestUserProvider,它在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()
            ]);
        }

并添加到购物车功能

    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 个答案:

没有答案