laravel transaction vs database statement

Viewed 210

what is the differences of following two:

DB::update(
    'update users set votes = 0 where votes < 0'
);
DB::transaction(function () {
    DB::table('users')->where('votes', '<' , 0)->update(['votes' => 0]);
});

Official Laravel doc says

The update method should be used to update existing records in the database

But transaction seems more handful that can react to exceptions.

So in what scenarios one is better than the other?

1 Answers

The DB::update() is for raw sql queries on the other hand DB::table()->update is for query builder. Query builder is much more efficient as it prepares and binds the statements itself.

The question has been answered here

Related