`Do not write information to database`
P粉321584263
P粉321584263 2024-03-22 12:33:37
0
1
607

Do not save to database Help me understand my error

Controller

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

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粉321584263
P粉321584263

reply all(1)
P粉323374878

My mutation happens to be in the wrong module

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
        })
    },
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template