1.laravel中使用事务
DB::beginTransaction();//开启事务 try { $row = LikesDetails::create(['article_id' => $article_id, 'review_id' => $review_id, 'member_id' => $member_id, 'update_time' => $time,'is_liked' => $is_liked, 'create_time' => $time]); $likes = Article::where(['id' => $article_id])->value('praise'); $likes += 1; Article::where(['id' => $article_id])->update(['praise' => $likes, 'update_time' => $time]); DB::commit();//提交事务 $message = '添加成功'; } catch (Exception $e) { DB::rollBack();//事务回滚 $message = '添加失败'; }
如果在事务的闭包中出现了异常,事务将会自动回滚。如果闭包执行成功,事务将会自动提交。在使用 transaction
方法时不需要手动回滚或提交
DB::transaction(function () { DB::update('update users set votes = 1'); DB::delete('delete from posts'); });
标签:事务,使用,DB,update,time,article,id From: https://www.cnblogs.com/junwuxiao/p/18082472