PHPackages                             socratesldduarte/laravel-database-encryption - 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. socratesldduarte/laravel-database-encryption

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

socratesldduarte/laravel-database-encryption
============================================

Auto Encrypt and Decrypt Database through Eloquent

1.1.1(5y ago)052MITPHP

Since Dec 12Pushed 4y agoCompare

[ Source](https://github.com/socratesldduarte/laravel-database-encryption)[ Packagist](https://packagist.org/packages/socratesldduarte/laravel-database-encryption)[ RSS](/packages/socratesldduarte-laravel-database-encryption/feed)WikiDiscussions main Synced yesterday

READMEChangelog (2)Dependencies (2)Versions (4)Used By (0)

Laravel Database Encryption Package
===================================

[](#laravel-database-encryption-package)

[![Latest Version on Packagist](https://camo.githubusercontent.com/ded516e0ce26555d59694e3e9d5894800336a69e6f35c27132277404be1e379c/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f736f6372617465736c646475617274652f6c61726176656c2d64617461626173652d656e6372797074696f6e2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/socratesldduarte/laravel-database-encryption)[![MIT Licensed](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE.md)[![Build Status](https://camo.githubusercontent.com/d968857f72eea6c26eb7745fcbc17cffe50ab2d5eb88d206a098740398e9812a/68747470733a2f2f7472617669732d63692e636f6d2f736f6372617465736c646475617274652f6c61726176656c2d64617461626173652d656e6372797074696f6e2e7376673f6272616e63683d6d61696e)](https://travis-ci.com/socratesldduarte/laravel-database-encryption)[![Total Downloads](https://camo.githubusercontent.com/b3f9b3d85a41ddfd442ab3adf0441506de90b462ce71781016f8038f90e95204/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f736f6372617465736c646475617274652f6c61726176656c2d64617461626173652d656e6372797074696f6e2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/socratesldduarte/laravel-database-encryption)

Package for encrypting and decrypting model attributes for Laravel using Laravel's Crypt
----------------------------------------------------------------------------------------

[](#package-for-encrypting-and-decrypting-model-attributes-for-laravel-using-laravels-crypt)

Key Features
------------

[](#key-features)

- Encrypt, Decrypt database fields easily
- Minimal configuration
- Include searching encrypted data using the following: `whereEncrypted` and `orWhereEncrypted`
- release 1.1 Includes ordering by encrypted data using the following: `orderByEncrypted`
- uses Laravel's Facades `Crypt` for encrypting and decrypting fields

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

[](#requirements)

- Laravel: &gt;= 5
- PHP: &gt;= 7.3

Schema Requirements
-------------------

[](#schema-requirements)

Encrypted values are usually longer than plain text values, sometimes much longer. You may find that the column widths in your database tables need to be altered to store the encrypted values generated by this package.

We highly recommend to alter your column types to `TEXT` or `LONGTEXT`

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

[](#installation)

### Step 1: Composer

[](#step-1-composer)

Via Composer command line:

```
$ composer require socratesldduarte/laravel-database-encryption
```

### Step 2: Add ServiceProvider to your app/config.php file (Laravel 5.4 or below)

[](#step-2-add-serviceprovider-to-your-appconfigphp-file-laravel-54-or-below)

Add the service provider to the providers array in the config/app.php config file as follows:

```
    'providers' => [
        ...
        \socratesldduarte\DBEncryption\Providers\DBEncryptionServiceProvider::class,
    ],
```

Usage
-----

[](#usage)

Use the `EncryptedAttribute` trait in any Eloquent model that you wish to apply encryption to and define a `protected $encrypted` array containing a list of the attributes to encrypt.

For example:

```

    use socratesldduarte\DBEncryption\Traits\EncryptedAttribute;

    class User extends Eloquent {
        use EncryptedAttribute;

        /**
         * The attributes that should be encrypted on save.
         *
         * @var array
         */
        protected $encryptable = [
            'first_name', 'last_name'
        ];
    }
```

By including the `EncryptedAttribute` trait, the `setAttribute()`, `getAttribute()` and `getAttributeFromArray()`methods provided by Eloquent are overridden to include an additional step.

### Searching Encrypted Fields Example:

[](#searching-encrypted-fields-example)

Searching encrypted field can be done by calling the `whereEncrypted` and `orWhereEncrypted` functions similar to laravel eloquent `where` and `orWhere`.

```
    namespace App\Http\Controllers;

    use App\User;
    class UsersController extends Controller {
        public function index(Request $request)
        {
            $user = User::whereEncrypted('first_name','john')
                        ->orWhereEncrypted('last_name','!=','Doe')->firstOrFail();

            return $user;
        }
    }
```

### Encrypt your current data

[](#encrypt-your-current-data)

If you have current data in your database you can encrypt it with the: `php artisan encryptable:encryptModel 'App\User'` command.

Additionally you can decrypt it using the: `php artisan encryptable:decryptModel 'App\User'` command.

Note: You must implement first the `Encryptable` trait and set `$encryptable` attributes

### Exists and Unique Validation Rules

[](#exists-and-unique-validation-rules)

If you are using exists and unique rules with encrypted values replace it with exists\_encrypted and unique\_encrypted `php       $validator = validator(['email'=>'foo@bar.com'], ['email'=>'exists_encrypted:users,email']); $validator = validator(['email'=>'foo@bar.com'], ['email'=>'unique_encrypted:users,email']); `

Frequently Asked Question
-------------------------

[](#frequently-asked-question)

#### Can I search encrypted data?

[](#can-i-search-encrypted-data)

YES! You will able to search on attributes which are encrypted by this package because. If you need to search on data then use the `whereEncrypted` and `orWhereEncrypted` function:

```
    User::whereEncrypted('email','test@gmail.com')->orWhereEncrypted('email','test2@gmail.com')->firstOrFail();

```

It will automatically added on the eloquent once the model uses `EncryptedAttribute`

#### Can I encrypt all my `User` model data?

[](#can-i-encrypt-all-my-user-model-data)

Aside from IDs you can encrypt everything you wan't

For example: Logging-in on encrypted email

```
$user = User::whereEncrypted('email','test@gmail.com')->filter(function ($item) use ($request) {
        return Hash::check($password, $item->password);
    })->where('active',1)->first();

```

Credits
-------

[](#credits)

This package was forked from the following: [elgibor-solution/laravel-database-encryption](https://github.com/elgibor-solution/laravel-database-encryption)This package was inspired from the following: [austinheap/laravel-database-encryption](https://github.com/austinheap/laravel-database-encryption)[magros/laravel-model-encryption](https://github.com/magros/laravel-model-encryption)[DustApplication/laravel-database-model-encryption](https://github.com/DustApplication/laravel-database-model-encryption.git)

License
-------

[](#license)

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

###  Health Score

25

—

LowBetter than 37% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity8

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity55

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 77.8% 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 ~76 days

Total

3

Last Release

1825d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/29414248?v=4)[socratesduarte](/maintainers/socratesduarte)[@SocratesDuarte](https://github.com/SocratesDuarte)

---

Top Contributors

[![socratesldduarte](https://avatars.githubusercontent.com/u/35846071?v=4)](https://github.com/socratesldduarte "socratesldduarte (7 commits)")[![elgibor-solution](https://avatars.githubusercontent.com/u/783039?v=4)](https://github.com/elgibor-solution "elgibor-solution (2 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/socratesldduarte-laravel-database-encryption/health.svg)

```
[![Health](https://phpackages.com/badges/socratesldduarte-laravel-database-encryption/health.svg)](https://phpackages.com/packages/socratesldduarte-laravel-database-encryption)
```

###  Alternatives

[doctrine/orm

Object-Relational-Mapper for PHP

10.2k285.3M6.2k](/packages/doctrine-orm)[jdorn/sql-formatter

a PHP SQL highlighting library

3.9k115.1M102](/packages/jdorn-sql-formatter)[illuminate/database

The Illuminate Database package.

2.8k52.4M9.4k](/packages/illuminate-database)[mongodb/mongodb

MongoDB driver library

1.6k64.0M546](/packages/mongodb-mongodb)[ramsey/uuid-doctrine

Use ramsey/uuid as a Doctrine field type.

90340.3M211](/packages/ramsey-uuid-doctrine)[reliese/laravel

Reliese Components for Laravel Framework code generation.

1.7k3.4M16](/packages/reliese-laravel)

PHPackages © 2026

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