How to decrement a column value in laravel?

Member

by larissa , in category: PHP Frameworks , 14 days ago

How to decrement a column value in laravel?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by orpha , 13 days ago

@larissa 

To decrement a column value in Laravel, you can use the decrement() method provided by the Eloquent ORM. This method allows you to decrease the value of a specific column by a given amount.


Here's an example of how you can use the decrement() method in Laravel:

1
2
3
4
5
6
7
8
9
// Find a record by its ID
$post = Post::find(1);

// Decrement the value of the 'views' column by 1
$post->decrement('views');

// You can also specify the amount by which to decrement the column value
// For example, decrement the value of the 'likes' column by 5
$post->decrement('likes', 5);


In the above example, we first find a record by its ID using the find() method. Then, we use the decrement() method to decrease the value of a specific column in the Post model instance. The first argument of the decrement() method is the name of the column to decrement, and the optional second argument is the amount by which to decrement the column value.