Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/Illuminate/Database/Concerns/ManagesTransactions.php
Original file line number Diff line number Diff line change
Expand Up @@ -353,4 +353,21 @@ public function afterCommit($callback)

throw new RuntimeException('Transactions Manager has not been set.');
}

/**
* Execute the callback after a transaction rolls back.
*
* @param callable $callback
* @return void
*
* @throws \RuntimeException
*/
public function afterRollBack($callback)
{
if ($this->transactionsManager) {
return $this->transactionsManager->addCallbackForRollback($callback);
}

throw new RuntimeException('Transactions Manager has not been set.');
}
}
1 change: 1 addition & 0 deletions src/Illuminate/Support/Facades/DB.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@
* @method static void rollBack(int|null $toLevel = null)
* @method static int transactionLevel()
* @method static void afterCommit(callable $callback)
* @method static void afterRollBack(callable $callback)
*
* @see \Illuminate\Database\DatabaseManager
*/
Expand Down
25 changes: 25 additions & 0 deletions tests/Integration/Database/DatabaseTransactionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,31 @@ public function testTransactionsDoNotAffectDifferentConnections()
$this->assertTrue($secondObject->ran);
$this->assertFalse($thirdObject->ran);
}

public function testAfterRollbackCallbacksAreExecuted()
{
$afterCommitRan = false;
$afterRollbackRan = false;

try {
DB::transaction(function () use (&$afterCommitRan, &$afterRollbackRan) {
DB::afterCommit(function () use (&$afterCommitRan) {
$afterCommitRan = true;
});

DB::afterRollBack(function () use (&$afterRollbackRan) {
$afterRollbackRan = true;
});

throw new \RuntimeException('rollback');
});
} catch (\RuntimeException) {
// Ignore the expected rollback exception.
}

$this->assertFalse($afterCommitRan);
$this->assertTrue($afterRollbackRan);
}
}

class TestObjectForTransactions
Expand Down