Lestah
149
1
Laravel

I didn't get anything when i use if(Auth::check())

Posted 3 months ago by Lestah

i have this controller before i was able to display all the items that are added on the cart

this my controller


public function cart()
    {   
        $session_id = Session::get('session_id');
        $userCart = DB::table('cart')->where(['session_id'=>$session_id])->get();
     
        //echo "<pre>"; print_r($userCart); die;

        //to get the image from product table
        foreach($userCart as $key => $product) {
            //to check if were getting the product_id
            //echo $product->product_id; die; 
            $productDetails = Product::where('id',$product->product_id)->first();
            //this will get the image
            $userCart[$key]->image = $productDetails->image;
        }
        return view('products.cart')->with(compact('userCart'));
    }

now i updated my controller to this to check if the user is logged in first


public function cart()
    {   
        if(Auth::check()){
            $user_email = Auth::user()->email;
            $userCart = DB::table('cart')->where(['user_email' => $user_email])->get();     
        }else{
            $session_id = Session::get('session_id');
            $userCart = DB::table('cart')->where(['session_id' => $session_id])->get();    
        }

        //to get the image from product table
        foreach($userCart as $key => $product) {
            //to check if were getting the product_id
            //echo $product->product_id; die; 
            $productDetails = Product::where('id',$product->product_id)->first();
            //this will get the image
            $userCart[$key]->image = $productDetails->image;
        }
        return view('products.cart')->with(compact('userCart'));
    }

now when i click add to cart button there's is no error it says i added the item but when i check the cart page there's no display of the item i added and then i tried to add it again it now says i already added the item on the cart

I dont have any errors what could be wrong? is it because of the if(Auth::check)?

Please sign in or create an account to participate in this conversation.

Reply to

Use Markdown with GitHub-flavored code blocks.