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.

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.

Read Also:  Ajax Upload Multiple Images without form submit

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:  AngularJS Ajax Live search

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.

Leave a Reply

Your email address will not be published. Required fields are marked *