How to Update Record Using Laravel Query Builder?

Updates are rather simple in Laravel if we are using query builder. We just need to create our query and instead of get() or first() we will add simply update(). Let’s code that.

DB::table( 'leads' )
    ->where( 'email', '=', '[email protected]' )
    ->update( [ 'age' => '38' ] );

We can also use increment or decrement methods if we are dealing with integer data. That is super easy and convenient. Like if we want to add one year for every record’s age then we can simply use increment() method. It’s super easy.

DB::table('leads')->increment('age');

Here if we will not provide second parameter to increment() method then it will increase by 1. Same we can decrement as well.

DB::table('leads')->decrement('points', 10);

I hope now update() makes sense to you in Laravel if you are using query builder. If you have any problem feel free to comment. We appreciate that. Enjoy learning.