search

Home  >  Q&A  >  body text

`Do not write information to database`

Do not save to database Help me understand my error

Controller

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

public function store(Request $request)

{

    $item = Cart::where('product_id', $request->product_id);

 

    if ($item->count()) {

        $item->increment('quantity');

        $item = $item->first();

    } else {

        $item = Cart::forceCreate([

            'product_id' => $request->product_id,

            'quantity' => 1,

        ]);

    }

 

    return response()->json([

        'quantity' => $item->quantity,

        'product' => $item->product

    ]);

}

shop

1

2

3

4

5

6

addProductToCart (product, quantity) {

        axios.post('http://127.0.0.1:8000/api/cart', {

            product_id: product.id,

            quantity

        })

},

Items are not added to the database when the addItems (for example) button is clicked

P粉321584263P粉321584263399 days ago707

reply all(1)I'll reply

  • P粉323374878

    P粉3233748782024-03-23 10:05:16

    My mutation happens to be in the wrong module

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    add_to_cart(state, {product, quantity}) {

     

            let productInCart = state.cart.find(item => {

                return item.product.id === product.id;

            });

     

            if (productInCart) {

                productInCart.quantity += quantity;

                return;

            }

     

            state.cart.push({

                product,

                quantity

            })

        },

    reply
    0
  • Cancelreply