how to use increment() and decrement() in laravel Eloquent? – laravel increment

Today, We want to share with you laravel increment.In this post we will show you laravel eloquent, hear for laravel collection filter we will give you demo and example for implement.In this post, we will learn about Increment Decrement With Laravel with an example.

Laravel increment or decrement column value example

we will use laravel eloquent increment() for increase value and we will use laravel 6 eloquent decrement() for decrease value of database table.

Laravel Increment and Decrement Example
Laravel Increment and Decrement Example

But Laravel made this task very easy with increment and decrement function.

Read Also:  Eloquent orwhere Query Use in Laravel

Increment Example

Example 1:

$client = Client::find($id);

$guests = $client->guests + 1;
$client->update(['guests' => $guests]);

In this example use increment() function to increment column value in Lravel 6.

Example 2:

Client::find($id)->increment('guests');

If you increment 2 then client increment() function in argument set 2.

Example 3:

Client::find($id)->increment('guests',2);

Decrement Example

Example 1:

Client::find($id)->decrement('guests');

Example 2:

Client::find($id)->decrement('guests',2);

Example 1:

   DB::table('clients')->increment('guests');

Example 2:

   DB::table('clients')->decrement('guests');

You can also achieve this by update query.
Example 1:

   Client::where('id', $id)->update(['guests' => DB::raw('guests + 1')]);

Example 2:

   Client::where('id', $id)->update(['guests' => DB::raw('guests - 1')]);

Decrement a column value Laravel

Article::find($id)->decrement('guests');
OR
Article::where('id',1)->decrement("guests");

If you want to customize column decrement value then you can pass the second argument in the decrement() function like below:

Article::find($id)->decrement('guests', 5);
OR
Article::where('id',1)->decrement("guests", 5); 

Increment Or Decrement Without Using Laravel Methods

Read Also:  Laravel Grouping Cache Offset & Limit Eloquent methods

Increment column value by

Article::where('id', $id)->update(['guests' => DB::raw('guests + 1')]);

Decrement column value by

Article::where('id', $id)->update(['guests' => DB::raw('guests - 1')]);

Increment a column value Laravel


Article::find($id)->increment('guests');
OR
Article::where('id',1)->increment("guests");   

customize column increment value

Article::find($id)->increment('guests', 5);
OR
Article::where('id',1)->increment("guests", 5);  

I hope you get an idea about laravel query builder.
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.