Increments and Decrements in Laravel Eloquent Model
Add to cart option is one of the most important functions in e-commerce websites. The tricky part is getting the number of items in the cart to display on the cart icon. The predominant approach to get this done is using the increment and decrement function on Laravel. This also facilitates the addition or removal of a product from your cart. The way to implement this function is ,
$user = User::find(‘517c43667db388101e00000f’);
$user->cart_count++;
// $user->cart_count--; // for decrement the count
$user->save()
An alternate and easier way is,
$user = User::find($article_id);
$user->increment('cart_count');
Also these will work:
$user->increment('cart_count');// increase one count
$user->decrement('cart_count'); // decrease one count
$user->increment('cart_count',10); // increase 10 count
$user->decrement('cart_count',10); // decrease 10 count