app/Http/Controllers/InShoppingCartsController.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)
    {
         if(Auth::user()){
                    $shopping_cart = Auth::user()->shoppingCart;
                    $response = InShoppingCart::create([
                                    'shopping_cart_id' => $shopping_cart->id,
                                    'article_id' => $request->article_id]);
                    if($request->ajax()){
                        return response()->json([
                            'products_count' => InShoppingCart::productsCount($shopping_cart->id)
                        ]);
                    }
                    if($response){
                        return redirect('/carrito');
                    }else{
                        return back();
                    }
         }else{
                    $shopping_cart_id = \Session::get('shopping_cart_id');
                    $shopping_cart = ShoppingCart::findOrCreateBySessionID($shopping_cart_id);
                    $response = InShoppingCart::create([
                            'shopping_cart_id' => $shopping_cart->id,
                            'article_id' => $request->article_id]);
                    if($request->ajax()){
                        return response()->json([
                            'products_count' => InShoppingCart::productsCount($shopping_cart->id)
                        ]);
                    }
                    if($response){
                        return redirect('/carrito');
                    }else{
                        return back();
                    }
        }
    }
public function store ( Request $ request ) { if ( Auth :: user ( ) ) { $ shopping_cart = Auth :: user ( ) -> shoppingCart ; $ response = InShoppingCart :: create ( [ 'shopping_cart_id' => $ shopping_cart -> id , 'article_id' => $ request -> article_id ] ) ; if ( $ request -> ajax ( ) ) { return response ( ) -> json ( [ 'products_count' => InShoppingCart :: productsCount ( $ shopping_cart -> id ) ] ) ; } if ( $ response ) { return redirect ( '/carrito' ) ; } else { return back ( ) ; } } else { $ shopping_cart_id = \ Session :: get ( 'shopping_cart_id' ) ; $ shopping_cart = ShoppingCart :: findOrCreateBySessionID ( $ shopping_cart_id ) ; $ response = InShoppingCart :: create ( [ 'shopping_cart_id' => $ shopping_cart -> id , 'article_id' => $ request -> article_id ] ) ; if ( $ request -> ajax ( ) ) { return response ( ) -> json ( [ 'products_count' => InShoppingCart :: productsCount ( $ shopping_cart -> id ) ] ) ; } if ( $ response ) { return redirect ( '/carrito' ) ; } else { return back ( ) ; } } }
Store a newly created resource in storage.
Store a newly created resource in storage .