app/Http/Controllers/CartsController.php
/**
        * Store a newly created resource in storage.
        *
        * @param \Illuminate\Http\Request $request
        * @return \Illuminate\Http\Response
    */
Store a newly created resource in storage.
Store a newly created resource in storage .
public function store(Request $request)
    {
        $user = Auth::id();
        $cek = Cart::where('user_id', '=', $user)
                    ->where('product_id', '=', $request->product_id)
                    ->where('carts.status', '=', 'notyet')->first();
        $product = Product::where('id', '=', $request->product_id)->first();
        if (is_null($cek)) {
            DB::table('carts')->insert(
                ['user_id' => $user,
                'product_id' => $request->product_id,
                'qty' => $request->qty,
                'status' => 'notyet']
            );
            DB::table('products')
            ->where('id', $request->product_id)
            ->update([
                'stock' => $product->stock - $request->qty
            ]);
            return redirect('/cart');
        }else{
            DB::table('carts')
            ->where('product_id', $request->product_id)
            ->update([
                'qty' => $cek->qty + $request->qty     
            ]);
            return redirect('/cart');
        } 
    }
public function store ( Request $ request ) { $ user = Auth :: id ( ) ; $ cek = Cart :: where ( 'user_id' , '=' , $ user ) -> where ( 'product_id' , '=' , $ request -> product_id ) -> where ( 'carts.status' , '=' , 'notyet' ) -> first ( ) ; $ product = Product :: where ( 'id' , '=' , $ request -> product_id ) -> first ( ) ; if ( is_null ( $ cek ) ) { DB :: table ( 'carts' ) -> insert ( [ 'user_id' => $ user , 'product_id' => $ request -> product_id , 'qty' => $ request -> qty , 'status' => 'notyet' ] ) ; DB :: table ( 'products' ) -> where ( 'id' , $ request -> product_id ) -> update ( [ 'stock' => $ product -> stock - $ request -> qty ] ) ; return redirect ( '/cart' ) ; } else { DB :: table ( 'carts' ) -> where ( 'product_id' , $ request -> product_id ) -> update ( [ 'qty' => $ cek -> qty + $ request -> qty ] ) ; return redirect ( '/cart' ) ; } }
Store a newly created resource in storage.
Store a newly created resource in storage .