PHPackages                             dinhdjj/laravel-auto-db-transaction-middleware - 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. dinhdjj/laravel-auto-db-transaction-middleware

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

dinhdjj/laravel-auto-db-transaction-middleware
==============================================

A laravel-middleware auto activate db-transaction on each request

v1.0.1(4y ago)05[3 PRs](https://github.com/divndev/laravel-auto-db-transaction-middleware/pulls)MITPHPPHP ^8.1

Since May 10Pushed 2y ago1 watchersCompare

[ Source](https://github.com/divndev/laravel-auto-db-transaction-middleware)[ Packagist](https://packagist.org/packages/dinhdjj/laravel-auto-db-transaction-middleware)[ Docs](https://github.com/dinhdjj/laravel-auto-db-transaction-middleware)[ RSS](/packages/dinhdjj-laravel-auto-db-transaction-middleware/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (2)Dependencies (12)Versions (6)Used By (0)

A laravel-middleware auto activate db-transaction on each request
=================================================================

[](#a-laravel-middleware-auto-activate-db-transaction-on-each-request)

[![Latest Version on Packagist](https://camo.githubusercontent.com/df8b4b918ad6a1b6c23455e719270f69374482cfae705059ba5f489c698f8240/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f64696e68646a6a2f6c61726176656c2d6175746f2d64622d7472616e73616374696f6e2d6d6964646c65776172652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/dinhdjj/laravel-auto-db-transaction-middleware)[![GitHub Tests Action Status](https://camo.githubusercontent.com/3d6be951189de36888b5bfdb3ba1df0ae0d163dcab3ee7dcb73be3a0ed40cbbf/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f776f726b666c6f772f7374617475732f64696e68646a6a2f6c61726176656c2d6175746f2d64622d7472616e73616374696f6e2d6d6964646c65776172652f72756e2d74657374733f6c6162656c3d7465737473)](https://github.com/dinhdjj/laravel-auto-db-transaction-middleware/actions?query=workflow%3Arun-tests+branch%3Amain)[![GitHub Code Style Action Status](https://camo.githubusercontent.com/67a0e799d48d9aa26e2e1516d72e68a5b2f7637f199e404cf465952fb32f8950/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f776f726b666c6f772f7374617475732f64696e68646a6a2f6c61726176656c2d6175746f2d64622d7472616e73616374696f6e2d6d6964646c65776172652f436865636b253230262532306669782532307374796c696e673f6c6162656c3d636f64652532307374796c65)](https://github.com/dinhdjj/laravel-auto-db-transaction-middleware/actions?query=workflow%3A%22Check+%26+fix+styling%22+branch%3Amain)[![Total Downloads](https://camo.githubusercontent.com/50ad43203f2ccc1b12526db5619805243552609c16dd2d93d95986ad37198ae0/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f64696e68646a6a2f6c61726176656c2d6175746f2d64622d7472616e73616374696f6e2d6d6964646c65776172652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/dinhdjj/laravel-auto-db-transaction-middleware)

This is where your description should go. Limit it to a paragraph or two. Consider adding a small example.

Requirements
------------

[](#requirements)

```
* Laravel 9+
* php 8.1+

```

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

[](#installation)

You can install the package via composer:

```
composer require dinhdjj/laravel-auto-db-transaction-middleware
```

Usage
-----

[](#usage)

Firstly you should register the middleware to group or you can use middleware in specific routes.

```
/**
 * The application's route middleware groups.
 *
 * @var  array
 */
protected $middlewareGroups = [
    'web' => [
        ...,
        \Dinhdjj\AutoDBTransaction\AutoDBTransactionMiddleware::class,
    ],

    'api' => [
        ...,
        \Dinhdjj\AutoDBTransaction\AutoDBTransactionMiddleware::class,
    ]
```

Above is all thing you need to do.

How it works
------------

[](#how-it-works)

Below I will show you how it auto activate db-transaction on each request.

1. It only activate `beginTransaction` on method `POST`, `PUT`, `PATCH`, `DELETE`...(not `GET`) methods.
2. In all cases it will auto `commit` and only `rollback` when it encounter an unhandled exception.
3. It will also throw exceptions in some cases.
    - When you miss `commit` or `rollback` on your own `beginTransaction`.
    - When you use redundant `commit` or `rollback` db-transaction.

Exception handler
-----------------

[](#exception-handler)

When an exception is thrown, in most cases behaver of this package will `rollBack` the db-transaction, but if you not use `default logging exception handler of laravel` the package will evaluate that you handled the exception and it will continue `commit` db-transaction.

Below is the example of cases not use `default logging exception handler of laravel`

```
//App\Exceptions\Handler

$this->reportable(function (InvalidOrderException $e) {
    //
})->stop();

$this->reportable(function (InvalidOrderException $e) {
    return false;
});
```

or

```
namespace App\Exceptions;

use Exception;

class InvalidOrderException extends Exception
{
    /**
     * Report the exception.
     *
     * @return bool|null
     */
    public function report()
    {
        return true;
        // or
        return null;
    }
}
```

If you not use `default logging exception handler of laravel` and you want to `rollBack` the db-transaction, you can use this:

```
// 1. use your own db-transaction
DB::beginTransaction();
// your code
DB::rollBack();
```

```
// 2. You helper method to rollback the package's db-transaction
$this->reportable(function (InvalidOrderException $e) {
    \Dinhdjj\AutoDBTransaction\Facades::rollBack();
})->stop();
```

Testing
-------

[](#testing)

```
composer test
```

Changelog
---------

[](#changelog)

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](https://github.com/spatie/.github/blob/main/CONTRIBUTING.md) for details.

Security Vulnerabilities
------------------------

[](#security-vulnerabilities)

Please review [our security policy](../../security/policy) on how to report security vulnerabilities.

Credits
-------

[](#credits)

- [dinhdjj](https://github.com/dinhdjj)
- [All Contributors](../../contributors)

License
-------

[](#license)

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

###  Health Score

26

—

LowBetter than 43% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity4

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity61

Established project with proven stability

 Bus Factor2

2 contributors hold 50%+ of commits

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.

###  Release Activity

Cadence

Every ~0 days

Total

2

Last Release

1463d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/47e2161449a748d8324f83eca75917aac149ae95ced37796d72a1da71be7df90?d=identicon)[dileedotdev](/maintainers/dileedotdev)

---

Top Contributors

[![dinwwwh](https://avatars.githubusercontent.com/u/64189902?v=4)](https://github.com/dinwwwh "dinwwwh (11 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (7 commits)")[![github-actions[bot]](https://avatars.githubusercontent.com/in/15368?v=4)](https://github.com/github-actions[bot] "github-actions[bot] (7 commits)")

---

Tags

laravellaravel middlewaredatabase transactiondinhdjjlaravel-auto-db-transaction-middleware

###  Code Quality

TestsPest

### Embed Badge

![Health badge](/badges/dinhdjj-laravel-auto-db-transaction-middleware/health.svg)

```
[![Health](https://phpackages.com/badges/dinhdjj-laravel-auto-db-transaction-middleware/health.svg)](https://phpackages.com/packages/dinhdjj-laravel-auto-db-transaction-middleware)
```

###  Alternatives

[dyrynda/laravel-model-uuid

This package allows you to easily work with UUIDs in your Laravel models.

4802.8M8](/packages/dyrynda-laravel-model-uuid)[spatie/laravel-model-flags

Add flags to Eloquent models

4301.1M1](/packages/spatie-laravel-model-flags)[clickbar/laravel-magellan

This package provides functionality for working with the postgis extension in Laravel.

423715.4k1](/packages/clickbar-laravel-magellan)[spatie/laravel-sql-commenter

Add comments to SQL queries made by Laravel

1931.4M1](/packages/spatie-laravel-sql-commenter)[spatie/laravel-deleted-models

Automatically copy deleted records to a separate table

409109.8k4](/packages/spatie-laravel-deleted-models)[wnx/laravel-backup-restore

A package to restore database backups made with spatie/laravel-backup.

203330.1k2](/packages/wnx-laravel-backup-restore)

PHPackages © 2026

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