使用使用
插入语句的事务
<?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; }