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 1mo ago

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

16

—

LowBetter than 5% of packages

Maintenance37

Infrequent updates — may be unmaintained

Popularity5

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity14

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://www.gravatar.com/avatar/68a2de33e071e363b097a1a971fe5c12d4851f001f60c7f7ad2cccdd41014756?d=identicon)[nirav5920](/maintainers/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

[doctrine/orm

Object-Relational-Mapper for PHP

10.2k285.3M6.2k](/packages/doctrine-orm)[jdorn/sql-formatter

a PHP SQL highlighting library

3.9k115.1M102](/packages/jdorn-sql-formatter)[illuminate/database

The Illuminate Database package.

2.8k52.4M9.3k](/packages/illuminate-database)[mongodb/mongodb

MongoDB driver library

1.6k64.0M543](/packages/mongodb-mongodb)[ramsey/uuid-doctrine

Use ramsey/uuid as a Doctrine field type.

90340.3M209](/packages/ramsey-uuid-doctrine)[reliese/laravel

Reliese Components for Laravel Framework code generation.

1.7k3.4M16](/packages/reliese-laravel)

PHPackages © 2026

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