'`does not write information to the database`
does not save to database
help me understand my mistake
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
]);
}
Store
addProductToCart (product, quantity) {
axios.post('http://127.0.0.1:8000/api/cart', {
product_id: product.id,
quantity
})
},
when clicking on the addItems(for instance) button, the items are not added to the database
Solution 1:[1]
my mutation happened 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
})
},
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Vadim Yanyushkin |
