In fact, there are only two ways to do this, and not particularly nice, because DB: transaction does not report errors.
Place the try / catch block inside the closure and set the external variable in the catch block if the transaction fails.
Perform a manual transaction using DB :: beginTransaction and rollback / commit, again with an exception handler according to this example:
DB :: beginTransaction ();
try {
$project = Project::find($id);
$project->users()->detach();
$project->delete();
DB::commit();
$success = true;
} catch (\Exception $e) {
$success = false;
DB::rollback();
}
if ($success) {
// the transaction worked ...
}