PHPackages                             nirav5920/enforce-db-transactions - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. nirav5920/enforce-db-transactions

ActiveLibrary[Database &amp; ORM](/categories/database)

nirav5920/enforce-db-transactions
=================================

Laravel middleware to enforce database transactions for multiple write operations

13PHP

Since Apr 9Pushed 1y ago1 watchersCompare

[ Source](https://github.com/nirav5920/enforce-db-transactions)[ Packagist](https://packagist.org/packages/nirav5920/enforce-db-transactions)[ RSS](/packages/nirav5920-enforce-db-transactions/feed)WikiDiscussions main Synced today

READMEChangelogDependenciesVersions (1)Used By (0)

Enforce DB Transactions
=======================

[](#enforce-db-transactions)

A Laravel middleware that automatically detects and enforces database transactions for multiple write operations within a single request.

Problem Statement
-----------------

[](#problem-statement)

In Laravel applications, when multiple write operations (INSERT, UPDATE, DELETE) are performed in a single request, they should be wrapped in a database transaction to ensure data integrity. However, developers often forget to do this, leading to potential data inconsistencies.

This package helps enforce good database practices by:

1. Detecting requests with multiple write operations
2. Logging errors in production/staging when transactions are missing
3. Throwing exceptions in development to alert developers

Installation
------------

[](#installation)

You can install the package via composer:

```
composer require nirav5920/enforce-db-transactions
```

Configuration
-------------

[](#configuration)

Publish the configuration file:

```
php artisan vendor:publish --provider="Nirav5920\EnforceDbTransactions\EnforceDbTransactionsServiceProvider" --tag="config"
```

This will create a `config/enforce-db-transactions.php` file where you can customize the excluded paths:

```
return [
    'excluded_paths' => [
        'api/health-check',
        '_debugbar/*',
        'telescope/*',
        'horizon/*',
        'api/pos/login',
        // Add your custom excluded paths here
    ],
];
```

Usage
-----

[](#usage)

The middleware is automatically registered to both the `web` and `api` middleware groups.

If you prefer to manually register the middleware, you can remove it from the `web` and `api` groups in the configuration and add it to your route definitions:

```
// In app/Http/Kernel.php
protected $routeMiddleware = [
    // ...
    'enforce.transactions' => \Nirav5920\EnforceDbTransactions\Middleware\EnforceDbTransactions::class,
];

// In your routes file
Route::middleware('enforce.transactions')->group(function () {
    // Your routes here
});
```

How It Works
------------

[](#how-it-works)

The middleware:

1. Listens for database queries being executed
2. Counts the number of write operations (INSERT, UPDATE, DELETE, MERGE)
3. Checks if a transaction has been started
4. If multiple write operations are detected without a transaction:
    - In production/staging: Logs an error
    - In development: Throws an exception

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE) for more information.

###  Health Score

15

—

LowBetter than 3% of packages

Maintenance33

Infrequent updates — may be unmaintained

Popularity5

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity15

Early-stage or recently created project

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/95966060?v=4)[Nirav Pitroda](/maintainers/nirav5920)[@nirav5920](https://github.com/nirav5920)

---

Top Contributors

[![nirav5920](https://avatars.githubusercontent.com/u/95966060?v=4)](https://github.com/nirav5920 "nirav5920 (4 commits)")

### Embed Badge

![Health badge](/badges/nirav5920-enforce-db-transactions/health.svg)

```
[![Health](https://phpackages.com/badges/nirav5920-enforce-db-transactions/health.svg)](https://phpackages.com/packages/nirav5920-enforce-db-transactions)
```

###  Alternatives

[jdorn/sql-formatter

a PHP SQL highlighting library

3.9k117.2M118](/packages/jdorn-sql-formatter)[propel/propel1

Propel is an open-source Object-Relational Mapping (ORM) for PHP5.

8351.6M87](/packages/propel-propel1)[pgvector/pgvector

pgvector support for PHP

198741.5k12](/packages/pgvector-pgvector)[jfelder/oracledb

Oracle DB driver for Laravel

11518.4k](/packages/jfelder-oracledb)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
