app/Http/Controllers/CartController.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)
    {
        $request->validate([
            'quantity' => ['required','min:1'],
        ]);
        $product = Product::findOrFail($request->product_id);
        if ($request->hasCookie('ecom_laravel')) {
            $cookie_id = $request->cookie('ecom_laravel');
            if(Cart::where('cookie_id',$cookie_id)->where('product_id',$request->product_id)->exists()){
                return redirect('product-details/'.$product->product_slug.'/#alreadyAdded')->with('error','Already Added To Cart');
            }
            else{
                $cart = new Cart();
                $cart->cookie_id = $cookie_id;
                $cart->product_id = $request->product_id;
                $cart->quantity = $request->quantity;
                $cart->offer_price = $request->offer_price;
                $cart->save();
                return redirect('product-details/'.$product->product_slug.'/#alreadyAdded')->with('success','Added To Cart');
            }
        }
        else {
            $cookie_id = Cookie::queue('ecom_laravel', Str::random(15), 43200);
            $cart = new Cart();
            $cart->cookie_id = $cookie_id;
            $cart->product_id = $request->product_id;
            $cart->quantity = $request->quantity;
            $cart->offer_price = $request->offer_price;
            $cart->save();
            return back()->with('success','Added To Cart');
        }
    }
public function store ( Request $ request ) { $ request -> validate ( [ 'quantity' => [ 'required' , 'min:1' ] , ] ) ; $ product = Product :: findOrFail ( $ request -> product_id ) ; if ( $ request -> hasCookie ( 'ecom_laravel' ) ) { $ cookie_id = $ request -> cookie ( 'ecom_laravel' ) ; if ( Cart :: where ( 'cookie_id' , $ cookie_id ) -> where ( 'product_id' , $ request -> product_id ) -> exists ( ) ) { return redirect ( 'product-details/' . $ product -> product_slug . '/#alreadyAdded' ) -> with ( 'error' , 'Already Added To Cart' ) ; } else { $ cart = new Cart ( ) ; $ cart -> cookie_id = $ cookie_id ; $ cart -> product_id = $ request -> product_id ; $ cart -> quantity = $ request -> quantity ; $ cart -> offer_price = $ request -> offer_price ; $ cart -> save ( ) ; return redirect ( 'product-details/' . $ product -> product_slug . '/#alreadyAdded' ) -> with ( 'success' , 'Added To Cart' ) ; } } else { $ cookie_id = Cookie :: queue ( 'ecom_laravel' , Str :: random ( 15 ) , 43200 ) ; $ cart = new Cart ( ) ; $ cart -> cookie_id = $ cookie_id ; $ cart -> product_id = $ request -> product_id ; $ cart -> quantity = $ request -> quantity ; $ cart -> offer_price = $ request -> offer_price ; $ cart -> save ( ) ; return back ( ) -> with ( 'success' , 'Added To Cart' ) ; } }
Store a newly created resource in storage.
Store a newly created resource in storage .