Skip to content

Instantly share code, notes, and snippets.

@aambrozkiewicz
Created January 15, 2017 14:40
Show Gist options
  • Save aambrozkiewicz/5b38416fc84a824649b1a137bd4fa84c to your computer and use it in GitHub Desktop.
Save aambrozkiewicz/5b38416fc84a824649b1a137bd4fa84c 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;
}
}
@hugeps
Copy link

hugeps commented Sep 3, 2019

Out of interested - did you test it ?

@aambrozkiewicz
Copy link
Author

Sure, I've used it few projects before

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment