-
-
Save mohamad-supangat/5a0b297e1521abdb0ebcc52d491b4043 to your computer and use it in GitHub Desktop.
Laravel Middleware with Database transaction
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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