事务
DB::transaction(function () use ($userId, $numVotes) {
// Possibly failing DB query
DB::table('users')
->where('id', $userId)
->update(['votes' => $numVotes]);
// Caching query that we don't want to run if the above query fails
DB::table('votes')
->where('user_id', $userId)
->delete();
});DB::beginTransaction();
// Take database actions
if ($badThingsHappened) {
DB::rollBack();
}
// Take other database actions
DB::commit();Last updated