PHPackages                             testmonitor/eloquent-lockable - 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. testmonitor/eloquent-lockable

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

testmonitor/eloquent-lockable
=============================

Make Eloquent models read-only after they're locked. Prevents updates and deletes based on a `locked` attribute.

v1.0.0(9mo ago)33491MITPHPPHP ^8.2

Since May 1Pushed 7mo ago2 watchersCompare

[ Source](https://github.com/testmonitor/eloquent-lockable)[ Packagist](https://packagist.org/packages/testmonitor/eloquent-lockable)[ RSS](/packages/testmonitor-eloquent-lockable/feed)WikiDiscussions main Synced 2w ago

READMEChangelog (1)Dependencies (5)Versions (5)Used By (0)

Eloquent Lockable
=================

[](#eloquent-lockable)

[![Latest Stable Version](https://camo.githubusercontent.com/589cee659404728175b80ca6c0fa05e8cda2f019f63cf0b20354073476f52ba4/68747470733a2f2f706f7365722e707567782e6f72672f746573746d6f6e69746f722f656c6f7175656e742d6c6f636b61626c652f762f737461626c65)](https://packagist.org/packages/testmonitor/eloquent-lockable)[![CircleCI](https://camo.githubusercontent.com/013247c75fb4b570964f1b5d702692b7ecf0ec1a98348af01c54de9545a621d0/68747470733a2f2f696d672e736869656c64732e696f2f636972636c6563692f70726f6a6563742f6769746875622f746573746d6f6e69746f722f656c6f7175656e742d6c6f636b61626c652e737667)](https://circleci.com/gh/testmonitor/eloquent-lockable)[![codecov](https://camo.githubusercontent.com/f03e84ac1a0137899332e87b8b4536e9496844dcab872b07c49dc473f955247f/68747470733a2f2f636f6465636f762e696f2f67682f746573746d6f6e69746f722f656c6f7175656e742d6c6f636b61626c652f67726170682f62616467652e7376673f746f6b656e3d4b4f5644365158375044)](https://codecov.io/gh/testmonitor/eloquent-lockable)[![StyleCI](https://camo.githubusercontent.com/fc9d31ee9a95077b3269511d8445b29f9288f93b97b6301ce50dc7b3874d1864/68747470733a2f2f7374796c6563692e696f2f7265706f732f3936383132303532382f736869656c64)](https://styleci.io/repos/968120528)[![License](https://camo.githubusercontent.com/274c03101a1439d89fccc53abdd5c8effc03c868b37bee4ebef3942860b5b049/68747470733a2f2f706f7365722e707567782e6f72672f746573746d6f6e69746f722f656c6f7175656e742d6c6f636b61626c652f6c6963656e7365)](https://packagist.org/packages/eloquent-lockable)

Make Laravel Eloquent models read-only after they're locked. Prevents updates and deletes based on a "locked" attribute. Includes a trait and required interface to clearly define and enforce lockable models.

Table of Contents
-----------------

[](#table-of-contents)

- [Installation](#installation)
- [Usage](#usage)
- [Tests](#tests)
- [Changelog](#changelog)
- [Contributing](#contributing)
- [Credits](#credits)
- [License](#license)

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

[](#installation)

This package can be installed through Composer:

```
$ composer require testmonitor/eloquent-lockable
```

No need to publish anything — just use the trait and you’re good to go.

Usage
-----

[](#usage)

First, make sure to add a `locked` column to your model's table:

```
Schema::table('invoices', function (Blueprint $table) {
    $table->boolean('locked')->default(false);
});
```

Next, implement the Lockable trait and interface:

```
use Illuminate\Database\Eloquent\Model;
use TestMonitor\Lockable\Traits\Lockable;
use TestMonitor\Lockable\Contracts\IsLockable;

class Invoice extends Model implements IsLockable
{
    use Lockable;
}
```

That's it!

### Locking &amp; Unlocking

[](#locking--unlocking)

Now you can start locking and unlocking models:

```
$invoice->markLocked();
$invoice->markUnlocked();

$invoice->isLocked();    // true or false
```

### Exceptions

[](#exceptions)

Trying to update or delete a locked model will throw a ModelLockedException.

```
try {
    $invoice->update(['amount' => 999]);
} catch (ModelLockedException $exception) {
    $model = $exception->getModel();
}
```

### Temporary Locking

[](#temporary-locking)

Temporarily lock or unlock a model using a callback:

```
$invoice->whileLocked(function ($model) {
    // Model is locked inside this closure
});

$invoice->whileUnlocked(function ($model) {
    // Temporarily unlocked
});
```

These automatically restore the original lock state — even if an exception is thrown.

### Retrieving Locked and Unlocked Models

[](#retrieving-locked-and-unlocked-models)

Convenient query scopes are provided to filter locked and unlocked models:

```
Invoice::locked()->get();
Invoice::unlocked()->get();
```

These are local scopes and can be used just like any other Eloquent scope.

### Configurable Lock Column

[](#configurable-lock-column)

Want to use a different column like archived or readonly?

Override the getLockColumn() method in your model:

```
public function getLockColumn(): string
{
    return 'archived';
}
```

### Allow Deletion of Locked Models

[](#allow-deletion-of-locked-models)

If you want to allow deletion of locked models, override the canDeleteWhenLocked() method in your model:

```
public function canDeleteWhenLocked(): bool
{
    return true;
}
```

### Allow Modifying Specific Attributes While Locked

[](#allow-modifying-specific-attributes-while-locked)

Sometimes, you may want to allow certain attributes to be changed. To do this, override the getLockExceptions() method in your model:

```
public function getLockExceptions(): array
{
    return ['note'];
}
```

Only the attributes listed in getLockExceptions() may be modified while the model is locked.

Tests
-----

[](#tests)

The package contains integration tests. You can run them using PHPUnit.

```
$ vendor/bin/phpunit

```

Changelog
---------

[](#changelog)

Refer to [CHANGELOG](CHANGELOG.md) for more information.

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

[](#contributing)

Refer to [CONTRIBUTING](CONTRIBUTING.md) for contributing details.

Credits
-------

[](#credits)

- [Thijs Kok](https://www.testmonitor.com/)
- [Stephan Grootveld](https://www.testmonitor.com/)
- [Frank Keulen](https://www.testmonitor.com/)
- [All Contributors](../../contributors)

License
-------

[](#license)

The MIT License (MIT). Refer to the [License](LICENSE.md) for more information.

###  Health Score

38

—

LowBetter than 83% of packages

Maintenance60

Regular maintenance activity

Popularity19

Limited adoption so far

Community12

Small or concentrated contributor base

Maturity53

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 91.7% 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

Unknown

Total

1

Last Release

280d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/39f48c881813b7d3b044ca5660aa5ab9e60b5dd7c34ed4a47acbb11bd20b7593?d=identicon)[thijskok](/maintainers/thijskok)

---

Top Contributors

[![thijskok](https://avatars.githubusercontent.com/u/1344550?v=4)](https://github.com/thijskok "thijskok (33 commits)")[![stefanius](https://avatars.githubusercontent.com/u/2707905?v=4)](https://github.com/stefanius "stefanius (2 commits)")[![StyleCIBot](https://avatars.githubusercontent.com/u/11048387?v=4)](https://github.com/StyleCIBot "StyleCIBot (1 commits)")

---

Tags

laravelmodeleloquenttestmonitorlockable

###  Code Quality

TestsPHPUnit

Code StylePHP CS Fixer

### Embed Badge

![Health badge](/badges/testmonitor-eloquent-lockable/health.svg)

```
[![Health](https://phpackages.com/badges/testmonitor-eloquent-lockable/health.svg)](https://phpackages.com/packages/testmonitor-eloquent-lockable)
```

###  Alternatives

[mongodb/laravel-mongodb

A MongoDB based Eloquent model and Query builder for Laravel

7.1k8.0M87](/packages/mongodb-laravel-mongodb)[prettus/l5-repository

Laravel 8|9|10|11|12|13 - Repositories to the database layer

4.2k11.2M153](/packages/prettus-l5-repository)[kirschbaum-development/eloquent-power-joins

The Laravel magic applied to joins.

1.6k29.9M42](/packages/kirschbaum-development-eloquent-power-joins)[tucker-eric/eloquentfilter

An Eloquent way to filter Eloquent Models

1.8k5.0M31](/packages/tucker-eric-eloquentfilter)[spiritix/lada-cache

A Redis based, automated and scalable database caching layer for Laravel

592452.8k2](/packages/spiritix-lada-cache)[dyrynda/laravel-model-uuid

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

4833.0M9](/packages/dyrynda-laravel-model-uuid)

PHPackages © 2026

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