PHPackages                             sfolador/laravel-locked - 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. sfolador/laravel-locked

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

sfolador/laravel-locked
=======================

This package adds locking features to Eloquent Models

0.5.1(3y ago)41207↓66.7%2[4 PRs](https://github.com/sfolador/laravel-locked/pulls)1MITPHPPHP ^8.1CI passing

Since Nov 13Pushed 1mo ago1 watchersCompare

[ Source](https://github.com/sfolador/laravel-locked)[ Packagist](https://packagist.org/packages/sfolador/laravel-locked)[ Docs](https://github.com/sfolador/laravel-locked)[ GitHub Sponsors](https://github.com/sfolador)[ RSS](/packages/sfolador-laravel-locked/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (6)Dependencies (14)Versions (11)Used By (1)

Locked eloquent models
======================

[](#locked-eloquent-models)

[![Latest Version on Packagist](https://camo.githubusercontent.com/742cc650c64ccd2dce702afe222cca5cd1f76b164ff0869a6357d749f4648036/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f73666f6c61646f722f6c61726176656c2d6c6f636b65642e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/sfolador/laravel-locked)[![GitHub Tests Action Status](https://camo.githubusercontent.com/c3c2b545a8ab676d1796bafab9b1ecc560bc7e4ae1e7dc102d7cc7cd459309f3/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f73666f6c61646f722f6c61726176656c2d6c6f636b65642f72756e2d74657374732e796d6c3f6272616e63683d6d61696e)](https://img.shields.io/github/actions/workflow/status/sfolador/laravel-locked/run-tests.yml?branch=main)[![GitHub Code Style Action Status](https://camo.githubusercontent.com/a265e35da5165b0b044d519fb465c4ca94a28109c97e687c1e867a4a3f91983a/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f73666f6c61646f722f6c61726176656c2d6c6f636b65642f6669782d7068702d636f64652d7374796c652d6973737565732e796d6c3f6272616e63683d6d61696e266c6162656c3d636f64652532307374796c65)](https://img.shields.io/github/actions/workflow/status/sfolador/laravel-locked/fix-php-code-style-issues.yml?branch=main)[![Total Downloads](https://camo.githubusercontent.com/8243a1e415c0734d30b67b6429ee6a80320eb93692efb1d9389164e9651482fb/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f73666f6c61646f722f6c61726176656c2d6c6f636b65642e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/sfolador/laravel-locked)

[![Laravel Locked](https://camo.githubusercontent.com/bc1da85a1d18850bf132de16aa317693132f05a984d6417986384b450308a3fc/68747470733a2f2f73666f6c61646f722d6769746875622e73332e65752d736f7574682d312e616d617a6f6e6177732e636f6d2f4c6f636b65645f736d616c6c2e706e673f743d31)](https://camo.githubusercontent.com/bc1da85a1d18850bf132de16aa317693132f05a984d6417986384b450308a3fc/68747470733a2f2f73666f6c61646f722d6769746875622e73332e65752d736f7574682d312e616d617a6f6e6177732e636f6d2f4c6f636b65645f736d616c6c2e706e673f743d31)

A package to add locking features to Eloquent Models.

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

[](#installation)

You can install the package via composer:

```
composer require sfolador/laravel-locked
```

You can publish the config file with:

```
php artisan vendor:publish --tag="locked-config"
```

This is the contents of the published config file:

```
return [
   'locking_column' => 'locked_at',

    'default_namespace' => 'App\Models',

    'unlock_allowed' => true,
    'can_be_unlocked' => [
    ],

    'prevent_modifications_on_locked_objects' => false,
];
```

You can choose another default column name for the locking column by changing the `locking_column` value. The `default_namespace` value is used to automatically add the namespace to the model passed as an argument to the Command. See the Usage section for more details.

The `unlock_allowed` value is used to enable or disable the `unlock` command. If you set it to `false`, the `unlock` method will raise an exception. It's possible to add a *whitelist* of models that can be unlocked by setting the `can_be_unlocked` array. If the array is empty and the `unlock_allowed` value is `false`, no model can be unlocked.

The `prevent_modifications_on_locked_objects` value is used to forbid modifications on *locked* models. If you set it to `true`, an exception will be raised if you try to save/delete/replicated a *locked* model.

Command
-------

[](#command)

There is an artisan command to create a migration for a class, run the command with :

```
php artisan lock-add {classname} {--namespace=}
```

For example, if you want to add a locking column to the `User` model, you can run the command :

```
php artisan lock-add User
```

This will create a migration file in the `database/migrations` folder, you can then run the migration with :

```
php artisan migrate
```

The command accepts an optional `--namespace` parameter, to specify the namespace of the class, for example :

```
php artisan lock-add User --namespace=App\Models\SomeFolder
```

The default namespace for the command is `App\Models` but you can change it in the config file by modifying the `default_namespace` value.

Usage
-----

[](#usage)

Once created the migration, you can use the `Lockable` trait in your model.

```
use Sfolador\Locked\Traits\HasLocks;

class User extends Model
{
    use HasLocks;
}
```

this trait will add the following methods to your model :

- `lock()` : adds a lock to the model by setting the locking column to the current date
- `unlock()`: removes the lock by setting the locking column to null
- `isLocked()`: returns true if the model is locked, false otherwise
- `isUnlocked()`: returns true if the model is unlocked, false otherwise
- `isNotUnlocked()`: returns true if the model is not unlocked, false otherwise
- `isNotLocked()`: returns true if the model is not locked, false otherwise
- `toggleLock()`: toggles the lock state of the model

Example
-------

[](#example)

```
$user = User::find(1);
$user->lock();

//...

if ($user->isNotLocked()) {
   UserManager::update($user);
}
```

Todo
----

[](#todo)

- Add an option to forbid locking a model if it is already locked and raise an Exception
- Add an option to block notifications to the model if it is locked
- Add logging to locking/unlocking actions for auditing purposes
- Add an option to block the model saving if it is locked

Testing
-------

[](#testing)

```
composer test
```

Changelog
---------

[](#changelog)

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

Credits
-------

[](#credits)

- [sfolador](https://github.com/sfolador)

License
-------

[](#license)

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

###  Health Score

40

—

FairBetter than 88% of packages

Maintenance60

Regular maintenance activity

Popularity24

Limited adoption so far

Community14

Small or concentrated contributor base

Maturity51

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 61.4% 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 ~18 days

Total

6

Last Release

1183d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/54c6b8884e3bd4e86d26de024ca07094728adc7db2a8a483538222121b00f9f1?d=identicon)[sfolador](/maintainers/sfolador)

---

Top Contributors

[![sfolador](https://avatars.githubusercontent.com/u/36632?v=4)](https://github.com/sfolador "sfolador (54 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (20 commits)")[![github-actions[bot]](https://avatars.githubusercontent.com/in/15368?v=4)](https://github.com/github-actions[bot] "github-actions[bot] (14 commits)")

---

Tags

eloquentlaravellockunlocklaravelsfoladorlaravel-locked

###  Code Quality

TestsPest

Code StyleLaravel Pint

### Embed Badge

![Health badge](/badges/sfolador-laravel-locked/health.svg)

```
[![Health](https://phpackages.com/badges/sfolador-laravel-locked/health.svg)](https://phpackages.com/packages/sfolador-laravel-locked)
```

###  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)
