【问题标题】:laravel transaction vs database statement [duplicate]laravel事务与数据库语句[重复]
【发布时间】:2021-09-27 16:47:29
【问题描述】:

以下两者有什么区别:

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

官方 Laravel 文档说

更新方法应该用于更新数据库中的现有记录

但事务似乎更少数可以对异常做出反应。

那么在什么情况下一种比另一种更好?

【问题讨论】:

    标签: laravel laravel-5 eloquent


    【解决方案1】:

    DB::update() 用于原始 sql 查询,而 DB::table()->update 用于查询构建器。查询生成器本身准备和绑定语句,因此效率更高。

    问题已回答here

    【讨论】:

      猜你喜欢
      • 2015-02-12
      • 1970-01-01
      • 2015-11-08
      • 1970-01-01
      • 1970-01-01
      • 2021-09-30
      • 2013-11-23
      • 1970-01-01
      • 2015-08-28
      相关资源
      最近更新 更多