Example of Database Transactions in realodix/urlhub

File app/Http/Controllers/UrlRedirectController.php (link to Github)
use App\Models\Url;
use App\Services\UrlRedirectionService;
use Illuminate\Support\Facades\DB;

class UrlRedirectController extends Controller
{
    public function __invoke(UrlRedirectionService $service, string $key)
    {
        return DB::transaction(function () use ($service, $key) {
            $url = Url::whereKeyword($key)->firstOrFail();

            return $service->handleHttpRedirect($url);
        });
    }
}