You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

38 lines
836 B
PHP

<?php
namespace App\Http\Middleware;
use Closure;
use Exception;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
class Transactional
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @param string|null $guard
* @return mixed
* @throws Exception
*/
public function handle($request, Closure $next, $guard = null)
{
DB::beginTransaction();
$response = $next($request);
if($response instanceof BinaryFileResponse) {
return $next($request);
}
if($response instanceof Exception) {
DB::rollBack();
} else {
DB::commit();
}
return $response;
}
}