PHPackages                             pmingram/laravel-model-encryptor - 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. [Security](/categories/security)
4. /
5. pmingram/laravel-model-encryptor

ActiveLibrary[Security](/categories/security)

pmingram/laravel-model-encryptor
================================

A model trait for flexible encryption and decryption of data

v1.0.0(5y ago)242MITPHPPHP &gt;=7.0

Since Jan 16Pushed 5y ago1 watchersCompare

[ Source](https://github.com/pmingram/laravel-model-encryptor)[ Packagist](https://packagist.org/packages/pmingram/laravel-model-encryptor)[ RSS](/packages/pmingram-laravel-model-encryptor/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (1)DependenciesVersions (2)Used By (0)

Laravel Model Encryptor
=======================

[](#laravel-model-encryptor)

[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE.md)[![PHP Version](https://camo.githubusercontent.com/f1a5cfc90a686f8220cd28f1137aa7761610a4ffc652bc173f209dc10635d4ab/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f706d696e6772616d2f6c61726176656c2d6d6f64656c2d656e63727970746f722e7376673f7374796c653d666c61742d737175617265)](https://php.net)[![Latest Version on Packagist](https://camo.githubusercontent.com/0b31c73b82206c17533be7576d5990609dafceb6a7878a03b19c65938362e1d6/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f706d696e6772616d2f6c61726176656c2d6d6f64656c2d656e63727970746f722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/pmingram/laravel-model-encryptor)

A model trait for flexible encryption and decryption of data

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

[](#installation)

You can install the package via composer:

```
composer require pmingram/laravel-model-encryptor
```

Usage and Configuration
-----------------------

[](#usage-and-configuration)

This package is a trait that can be added to any Laravel model you wish to apply encryption to:

```
use PmIngram\Laravel\ModelEncryptor\HasEncryption;

class ModelName extends Model
{
    use HasEncryption;
}
```

By default, the trait will apply encryption to any record on creation - but to actually encrypt data, you need to configure some properties within your model:

PropertyTypeDescriptionDefault Value$encryptOnCreateBooleanEnable or disable encryption on model creation. If set to `false`, the model can be encrypted later by invoking `$model->encrypt(true)`.`true`$encryptionKeyStringA random string to act as a base encryption key for the model in conjunction with the application key set by Laravel. If left blank, the Laravel application key will be used alone.Empty string$encryptionSaltColumnStringThe data in the column defined here will be appended to the `$encryptionKey` to create a per-row encryption key. This should **not** be a column in the `$encryptionColumnKeys` array, as this could lead to data loss.Empty string$encryptionColumnKeysArrayList of columns to be included in the encryption and decryption processes. These columns should be defined as `LONGTEXT` datatypes (or the equivalent type in your database engine.)Empty arrayThere is no specific scope requirement for these properties, but it is recommended to use a `protected` scope.

Encryption and Decryption
-------------------------

[](#encryption-and-decryption)

Models can be encrypted and decrypted easily with a simple method call:

```
$model->encrypt();
$model->decrypt();
```

This will encrypt or decrypt the data within the current model instance, but will not persist the change to the database. This is particularly useful in the event you wish to decrypt the data for presentation (for example, in a view or in a resource for an API endpoint) but you want to keep that data encrypted in the database.

To persist to the database, simply pass `true` as an optional argument within the method:

```
$model->encrypt(true);
$model->decrypt(true);
```

Example Configuration
---------------------

[](#example-configuration)

### Model

[](#model)

```
encryptionKey = config('encryptionkeys.model.example');

        parent::__construct($attributes);
    }
}
```

### Laravel Configuration File - "encryptionkeys.php"

[](#laravel-configuration-file---encryptionkeysphp)

```
 [
        'example' => env('ENCKEY_MODEL_EXAMPLE', null),
    ]
];
```

### Environment Variable (.env)

[](#environment-variable-env)

```
ENCKEY_MODEL_EXAMPLE=somerandomstring
```

Recommendation on Encryption Keys and Security
----------------------------------------------

[](#recommendation-on-encryption-keys-and-security)

While it is entirely possible to store the model-level encryption key within the model itself, as a string in the `$encryptionKey`property, it is **strongly advised** to abstract the string out to a configuration file as per the example above, then use the `.env` file to set the strings.

It is both bad practice and a security risk to store encryption keys and passwords within a codebase, especially when that codebase is persisted to a VCS such as Git or SVN.

Important Note
--------------

[](#important-note)

The Laravel application key is used with this trait. If the application key is changed, **any encrypted data will no longer be readable**. Of course this is the case with any encryption routines deployed, but should be considered if you need to change your application's key.

License
-------

[](#license)

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

###  Health Score

24

—

LowBetter than 32% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity11

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity48

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

Unknown

Total

1

Last Release

1942d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/baa4c2f0adf0ec292fa3e727ad054f7ed05b30addff937235175deed923f73af?d=identicon)[pmingram](/maintainers/pmingram)

---

Top Contributors

[![paulmichaeldev](https://avatars.githubusercontent.com/u/6013735?v=4)](https://github.com/paulmichaeldev "paulmichaeldev (6 commits)")

### Embed Badge

![Health badge](/badges/pmingram-laravel-model-encryptor/health.svg)

```
[![Health](https://phpackages.com/badges/pmingram-laravel-model-encryptor/health.svg)](https://phpackages.com/packages/pmingram-laravel-model-encryptor)
```

###  Alternatives

[defuse/php-encryption

Secure PHP Encryption Library

3.9k162.4M214](/packages/defuse-php-encryption)[roave/security-advisories

Prevents installation of composer packages with known security vulnerabilities: no API, simply require it

2.9k97.3M6.4k](/packages/roave-security-advisories)[mews/purifier

Laravel 5/6/7/8/9/10 HtmlPurifier Package

2.0k16.7M113](/packages/mews-purifier)[robrichards/xmlseclibs

A PHP library for XML Security

41278.1M118](/packages/robrichards-xmlseclibs)[bjeavons/zxcvbn-php

Realistic password strength estimation PHP library based on Zxcvbn JS

86917.5M63](/packages/bjeavons-zxcvbn-php)[enlightn/security-checker

A PHP dependency vulnerabilities scanner based on the Security Advisories Database.

33732.2M110](/packages/enlightn-security-checker)

PHPackages © 2026

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