PHPackages                             ivanomatteo/laravel-db-mutex - 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. ivanomatteo/laravel-db-mutex

ActiveLibrary

ivanomatteo/laravel-db-mutex
============================

Your Package Description here

0.1.9(5y ago)7832MITPHP

Since Dec 23Pushed 5y ago1 watchersCompare

[ Source](https://github.com/ivanomatteo/laravel-db-mutex)[ Packagist](https://packagist.org/packages/ivanomatteo/laravel-db-mutex)[ RSS](/packages/ivanomatteo-laravel-db-mutex/feed)WikiDiscussions master Synced yesterday

READMEChangelog (10)Dependencies (7)Versions (11)Used By (0)

laravel-db-mutex
================

[](#laravel-db-mutex)

[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE.md)![Travis](https://camo.githubusercontent.com/86737daad46552378665503bc18e7cd76624c89a4ada2673ff5e7630463bcb1b/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f6976616e6f6d617474656f2f6c61726176656c2d64622d6d757465782e7376673f7374796c653d666c61742d737175617265)[![Total Downloads](https://camo.githubusercontent.com/a8a9fbfd77593000d748e7c476f024780a3bb15b5168a0b50952bb8934e9389a/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6976616e6f6d617474656f2f6c61726176656c2d64622d6d757465782e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/ivanomatteo/laravel-db-mutex)

This library implements a mutex mechanism, using a polymorphic "one to many" relationship.

When calling usingDbMutex(), if not alredy exists, a row matching the current model type, id and the specified "name" field (the default name is "default"), will be added in the db\_mutexes table.

On that row will be applied a "lock for update" (the db engine in use must support it), eusuring the mutual exclusion.

In this way you can avoid to put the lock on the table containing your data (possible bottle neck), preserving the read/write capability for all request that do not need a mutual exclusion.

Install
-------

[](#install)

```
composer require ivanomatteo/laravel-db-mutex

php artisan migrate
```

Usage
-----

[](#usage)

Write a few lines about the usage of this package.

```
// add HasDbMutex trait to your model

use HasDbMutex; // ( IvanoMatteo\LaravelDbMutex\HasDbMutex )

$m = YourModel::find(1);

$m->usingDbMutex(function(){
    // this code will run in mutual exclusion
    // for all request calling it
    // on the record with id 1
    sleep(5);
    echo "done!";
});

$m->usingDbMutex(function(){
    // this code will run in mutual exclusion
    // for all request calling it
    // on the record with id 1, with "foo" identifier
    sleep(5);
    echo "done!";
},null,"foo");

$m->usingDbMutex(function(){
    /*
        in this case we will use also an optimistic lock mechanism
        we can provide the previous value of
            - counter
                more reliable but slower, the counter value since is incremented inside a "read lock"
                can't never be the same

            and/or

            - model_updated_at (updated_at timestamp of the model)
                can be used if you are making modification on the model
                (model_updated_at would not make sense if your modifications are applied only to someting else)
                less reliable but faster, the updated_at field of the model
                can be read outside of the "read lock"

        if the values do not match the currents, a 412 http error will be returned

        NOTE: these values must came from the current REQUEST, not from the retrieved models
    */
},
    [
        'counter' => 10,
        'model_updated_at' => '2020-12-28 14:56:44',
    ]
);
// in the case, you want to use the counter value, obviously you need to load the previous value
// when reading the data. You could you use withDbMutex scope as explained below.

// there is also the  withDbMutex scope
YourModel::withDbMutex()->find(1); //will add the "default" dbmutex data
YourModel::withDbMutex('foo')->find(1); //will add the "foo" dbmutex data
```

### Warning

[](#warning)

When reading the model with the "dbmutex" relation information, it's possible that you have to wait for the lock became avaible on that rows.

It's recommended to load it, only if that one is necessary, for example if you need to use the optimistic lock mechanism with the counter value.

For the same reason, it's also recommended to load the minimum number of dbmutex related rows.

Testing
-------

[](#testing)

Run the tests with:

```
vendor/bin/phpunit
```

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

[](#contributing)

Please see [CONTRIBUTING](CONTRIBUTING.md) for details.

Security
--------

[](#security)

If you discover any security-related issues, please email  instead of using the issue tracker.

License
-------

[](#license)

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

###  Health Score

27

—

LowBetter than 49% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity21

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity49

Maturing project, gaining track record

 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.

###  Release Activity

Cadence

Every ~0 days

Total

10

Last Release

1960d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/35255310?v=4)[Ivano Matteo](/maintainers/IvanoMatteo)[@ivanomatteo](https://github.com/ivanomatteo)

---

Top Contributors

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

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/ivanomatteo-laravel-db-mutex/health.svg)

```
[![Health](https://phpackages.com/badges/ivanomatteo-laravel-db-mutex/health.svg)](https://phpackages.com/packages/ivanomatteo-laravel-db-mutex)
```

PHPackages © 2026

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