墨斋记

Good Luck To You!

Laravel处理mysql事务

使用使用

  1. 插入语句的事务


    <?php
    
    use Illuminate\Support\Facades\DB;
    
    DB::beginTransaction();
    
    try {
        DB::table('users')->insert([
            'name' => 'John Doe',
            'email' => 'johndoe@example.com',
            'password' => bcrypt('password'),
        ]);
    
        DB::table('orders')->insert([
            'user_id' => 1,
            'product' => 'Laravel book',
            'price' => 29.99,
        ]);
    
        DB::commit();
    } catch (\Exception $e) {
        DB::rollback();
        throw $e;
    }
<< 1 >>

Powered By Z-BlogPHP 1.7.3

冀公网安备13019902000834
冀ICP备18019600号-1
Copyright CC Some Rights Reserved.Contact Email:chengyingbo88@163.com