How to Increment and Decrement Column Value in Laravel 6?

Today, We want to share with you How to Increment and Decrement Column Value in Laravel 6?.In this post we will show you How to increment and update column in one eloquent query, hear for How to update table field = field+1 in laravel eloquent update query we will give you demo and example for implement.In this post, we will learn about How to increment a column using Eloquent Model in Laravel 6 with an example.

Read Also:  Database Data Export to Excel File using laravel

How to Increment and Decrement Column Value in Laravel 6?

There are the Following The simple About how to use increment() and decrement() in laravel 6 Full Information With Example and source code.

As I will cover this Post with live Working example to develop increment column value by 1 in mysql laravel 6, so the laravel 6 call to a member function increment() on integer is used for this example is following below.

Laravel 6 Increment column values

increment database column value bye one

public function increment($product_id)
{
	Product::find($product_id)->increment('total_likes');
	//OR
	Product::where('product_id', $product_id)->increment('total_likes');
}

specific number in increment() function.

public function increment($product_id)
{
	Product::find($product_id)->increment('total_likes', 5);
	//OR
	Product::where('product_id', $product_id)->increment('total_likes', 5);
}

Laravel 6 Decrement column values

decrement database column value help of decrement() function.

public function decrement($product_id)
{
	Product::find($product_id)->decrement('total_likes');
	//OR
	Product::where('product_id', $product_id)->decrement('total_likes');
}

specific number in decrement() function

public function decrement($product_id)
{
	Product::find($product_id)->decrement('total_likes', 5);
	//OR
	Product::where('product_id', $product_id)->decrement('total_likes', 5);
}

Increment Or Decrement Without Using Laravel 6 Methods

Increment column value :

public function increment($product_id)
{
	Product::where('product_id', $product_id)->update(['total_likes' => \DB::raw('total_likes + 1')]);
}

Decrement column value :

public function decrement($product_id)
{
	Product::where('product_id', $product_id)->update(['total_likes' => \DB::raw('total_likes - 1')]);
}

Web Programming Tutorials Example with Demo

Read :

Read Also:  Laravel Eloquent Update increment decrement Database column value

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about Super simple increment / decrement column value in laravel 6.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.


Related FAQ

Here are some more FAQ related to this Article:

  1. Read Also:  PHP Laravel Eloquent Increments and Decrements value
  2. Read Also:  how to get last inserted record in laravel?
  3. Read Also:  Laravel 6 Eloquent Model Increment Decrement
  4. Read Also:  Get Responsive navbar menu to Automatically close after anchor link clicked
  5. Read Also:  how to link one page to another page in html using button?
  6. Read Also:  Ajax File Upload without refreshing using jQuery and PHP
  7. Read Also:  sql subquery examples
  8. Read Also:  ReactJS - Overview For Beginners step by step
  9. Read Also:  Laravel Global Variables Example Tutorial From Scratch
  10. Read Also:  jQuery AJAX Interview Questions and Answers
CLOSEX