PHPackages                             pimphand/mysql-encrypted - 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. pimphand/mysql-encrypted

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

pimphand/mysql-encrypted
========================

Laravel package for encrypting and decrypting model attributes using openssl with MySQL support

16CI failing

Since Aug 22Pushed 10mo agoCompare

[ Source](https://github.com/pimphand/mysql-encrypted)[ Packagist](https://packagist.org/packages/pimphand/mysql-encrypted)[ RSS](/packages/pimphand-mysql-encrypted/feed)WikiDiscussions main Synced today

READMEChangelogDependenciesVersions (1)Used By (0)

mysql-encrypted
===============

[](#mysql-encrypted)

[![Latest Version on Packagist](https://camo.githubusercontent.com/14c657961e753a46dc2e64a01a35e8938fc81fdc3c7b84f3d93e508cf8ecfd83/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f70696d7068616e642f6d7973716c2d656e637279707465642e737667)](https://packagist.org/packages/pimphand/mysql-encrypted)[![MIT Licensed](https://camo.githubusercontent.com/074b89bca64d3edc93a1db6c7e3b1636b874540ba91d66367c0e5e354c56d0ea/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e737667)](LICENSE.md)[![Build Status](https://camo.githubusercontent.com/1c24d483a0715fd0cecf6840ff7b637bb3b7e2d1e548652a179063a1851cf61b/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f70696d7068616e642f6d7973716c2d656e637279707465642f6d61737465722e737667)](https://travis-ci.org/pimphand/mysql-encrypted)[![Total Downloads](https://camo.githubusercontent.com/0147f3ccc3273b90e49c83b896f847190f23e682de884358c9d5958438ed40da/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f70696d7068616e642f6d7973716c2d656e637279707465642e737667)](https://packagist.org/packages/pimphand/mysql-encrypted)

Package for encrypting and decrypting model attributes for Laravel using openssl

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

[](#key-features)

- Encrypt, Decrypt database fields easily
- Minimal configuration
- Include searching encrypted data using the following: `whereEncrypted` and `orWhereEncrypted`
- uses openssl for encrypting and decrypting fields

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

[](#requirements)

- Laravel: &gt;= 9.0
- PHP: &gt;= 8.1

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 pimphand/mysql-encrypted
```

### Step 2: ServiceProvider (Auto-discovered in Laravel 9.5+)

[](#step-2-serviceprovider-auto-discovered-in-laravel-95)

The service provider is automatically registered. For older Laravel versions, add to `config/app.php`:

```
'providers' => [
    ...
    \Pimphand\MysqlEncrypted\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 Pimphand\MysqlEncrypted\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`:

```
$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 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)

License
-------

[](#license)

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

###  Health Score

17

—

LowBetter than 6% of packages

Maintenance39

Infrequent updates — may be unmaintained

Popularity6

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity14

Early-stage or recently created project

 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.

### Community

Maintainers

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

---

Top Contributors

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

### Embed Badge

![Health badge](/badges/pimphand-mysql-encrypted/health.svg)

```
[![Health](https://phpackages.com/badges/pimphand-mysql-encrypted/health.svg)](https://phpackages.com/packages/pimphand-mysql-encrypted)
```

###  Alternatives

[jdorn/sql-formatter

a PHP SQL highlighting library

3.9k117.2M117](/packages/jdorn-sql-formatter)[propel/propel1

Propel is an open-source Object-Relational Mapping (ORM) for PHP5.

8351.6M87](/packages/propel-propel1)[jfelder/oracledb

Oracle DB driver for Laravel

11518.4k](/packages/jfelder-oracledb)

PHPackages © 2026

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