admin 发表于 2023-3-12 15:09:45

数据库事务机制如何使用

<h2>问题描述:</h2>

<p>事物机制如何启动呢,例如某个函数里可能增删改查有多个,如何启动事物机制呢</p><hr>以下是回复内容:<br><div class="ub-html content">
                                                    <p>闭包方式</p>

<pre class="brush:php;toolbar:false">DB::transaction(function () {
    DB::table('users')->update(['votes' => 1]);
    DB::table('posts')->delete();
});</pre>

<p>手动模式</p>

<pre class="brush:php;toolbar:false">// 开启事务
DB::beginTransaction();
// 回滚
DB::rollBack();
// 提交
DB::commit();</pre>

<p>具体可参照Laravel的标准用法</p>

<p><ahref="https://learnku.com/docs/laravel/5.1/eloquent/1080">https://learnku.com/docs/laravel/5.1/eloquent/1080</a> </p>

<p><br></p>
                                                </div><hr><br>采集网址:<br>https://modstart.com/thread/756
页: [1]
查看完整版本: 数据库事务机制如何使用