tapanderasari / laravel-mysql-encrypt
Laravel Database encryption mysql side
Package info
github.com/TapanDerasari/laravel-mysql-encrypt
pkg:composer/tapanderasari/laravel-mysql-encrypt
Requires
- php: ^8.3
- illuminate/database: ^13.0
- illuminate/support: ^13.0
Requires (Dev)
- orchestra/testbench: ^11.0
- pestphp/pest: ^4.0
- pestphp/pest-plugin-laravel: ^3.0|^4.0
- dev-master
- v3.0.0
- v1.0.9
- v1.0.8
- v1.0.7
- v1.0.6.x-dev
- v1.0.6
- v1.0.5
- v1.0.4
- v1.0.3
- v1.0.2
- v1.0.1
- 1.0.0
- dev-feature/laravel-13-support
- dev-special-chars-fix
- dev-change_encryptable_stuff_sandeep
- dev-implement-for-join-queries-sandeep
- dev-existing-data-encryption-sandeep-04-Jan-2024
- dev-revert-1-search-sort-filter-sandeep-12-dec
- dev-search-sort-filter-sandeep
This package is auto-updated.
Last update: 2026-03-18 05:24:08 UTC
README
Laravel database encryption at database side using native AES_DECRYPT and AES_ENCRYPT functions. Automatically encrypt and decrypt fields in your Models.
Version Compatibility
| Package Version | Laravel | PHP |
|---|---|---|
| 3.x | 13.x | ^8.3 |
| 1.x | 10–12 | ^8.0 |
Install
1. Composer
composer require tapanderasari/laravel-mysql-encrypt
2. Publish config (optional)
php artisan vendor:publish --provider="TapanDerasari\MysqlEncrypt\Providers\LaravelServiceProvider"
3. Set encryption key in .env file
APP_AESENCRYPT_KEY=yourencryptionkey
Update Models
<?php namespace App; use TapanDerasari\MysqlEncrypt\Traits\Encryptable; use Illuminate\Database\Eloquent\Model; class User extends Model { use Encryptable; // <-- 1. Include trait public array $encryptable = [ // <-- 2. Include columns to be encrypted 'email', 'first_name', 'last_name', 'telephone', ]; }
Schema
Encrypted columns must use VARBINARY (or BINARY/BLOB) in MySQL, not VARCHAR.
Schema::create('users', function (Blueprint $table) { $table->bigIncrements('id'); $table->string('password'); $table->binary('first_name', 300); // VARBINARY(300) $table->binary('last_name', 300); $table->binary('email', 300); $table->binary('telephone', 50); $table->rememberToken(); $table->timestamps(); });
Validators
unique_encrypted
unique_encrypted:<table>,<field(optional)>,<ignore_id(optional)>
exists_encrypted
exists_encrypted:<table>,<field(optional)>
Scopes
A global scope DecryptSelectScope is automatically applied to models using the Encryptable trait. It rewrites SELECT queries to wrap encrypted columns with AES_DECRYPT().
The following local scopes are available:
Exact match
User::whereEncrypted('email', 'john@example.com')->first(); User::whereNotEncrypted('email', 'john@example.com')->get();
OR conditions
User::whereEncrypted('email', 'a@b.com') ->orWhereEncrypted('email', 'c@d.com') ->get(); User::whereNotEncrypted('email', 'a@b.com') ->orWhereNotEncrypted('email', 'c@d.com') ->get();
LIKE search
User::whereEncryptedLike('first_name', 'John')->get(); User::orWhereEncryptedLike('first_name', 'Jane')->get();
Ordering
User::orderByEncrypted('first_name', 'asc')->get(); User::orderByEncryptedSort('last_name', 'desc')->get();
Implementing encryption for existing data
For this you can create one command like
php artisan make:command EncryptionForExistingData
In this command you fetch existing table or model data without global scope DecryptSelectScope.
You can refer the example, clicking on below Example button:
License
The MIT License (MIT). Please see License File for more information.