Skip to content

Instantly share code, notes, and snippets.

@mohamad-supangat
Forked from aambrozkiewicz/Transactional.php
Created September 13, 2023 03:35
Show Gist options
  • Save mohamad-supangat/5a0b297e1521abdb0ebcc52d491b4043 to your computer and use it in GitHub Desktop.
Save mohamad-supangat/5a0b297e1521abdb0ebcc52d491b4043 to your computer and use it in GitHub Desktop.
Laravel Middleware with Database transaction
<?php
namespace App\Http\Middleware;
use Closure;
class Transactional
{
public function handle($request, Closure $next)
{
\DB::beginTransaction();
$response = $next($request);
if ($response->exception) {
\DB::rollBack();
} else {
\DB::commit();
}
return $response;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment