laravel的事务使用如下:

DB::connection('gvideo')->transaction(function () use ($user_id, $video_id, $action_id) {
                $this->create(array('user_id' => $user_id, 'video_id' => $video_id, 'action_id' => $action_id));
                
                if (UserTotalActionRecord::where('user_id', $user_id)->where('action_id', $action_id)->where('record_date', date("Y-m-d"))->first()) {
                    UserTotalActionRecord::where('user_id', $user_id)->where('action_id', $action_id)->where('record_date', date("Y-m-d"))->increment('action_times');
                } else {
                    UserTotalActionRecord::create(array('user_id' => $user_id, 'action_id' => $action_id, 'record_date' => date("Y-m-d")));
                }
            });

之前使用时发现匿名函数没用 use 来引用外部变量,使得在函数体内使用时会出错,因此需注重匿名函数的使用。

相关文章:

  • 2021-05-23
  • 2022-01-24
  • 2022-12-23
  • 2022-12-23
  • 2021-08-16
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-07-10
  • 2022-12-23
  • 2022-03-04
  • 2022-12-23
  • 2021-10-31
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案