Laravel/Lumen database encryption at database side using native AES_DECRYPT and AES_ENCRYPT functions. Automatically encrypt and decrypt fields in your Models.
composer require PrajapatiDhara1510/laravel-mysql-encrypt
Laravel
php artisan vendor:publish --provider="PrajapatiDhara1510\MysqlEncrypt\Providers\LaravelServiceProvider"
Lumen
mkdir -p config
cp vendor/prajapatidhara1510/laravel-mysql-encrypt/config/config.php config/mysql-encrypt.php
Laravel
-
For Laravel 5.5 or later, the service provider is automatically loaded, skip this step.
-
For Laravel 5.4 or earlier, add the following to
config/app.php
:
'providers' => array(
PrajapatiDhara1510\\MysqlEncrypt\\Providers\\LaravelServiceProvider::class
);
Lumen
- For Lumen, add the following to
bootstrap/app.php
:
$app->register(PrajapatiDhara1510\MysqlEncrypt\Providers\LumenServiceProvider::class);
APP_AESENCRYPT_KEY=yourencryptionkey
<?php
namespace App;
use PrajapatiDhara1510\MysqlEncrypt\Traits\Encryptable;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
use Encryptable; // <-- 1. Include trait
protected $encryptable = [ // <-- 2. Include columns to be encrypted
'email',
'first_name',
'last_name',
'telephone',
];
}
unique_encrypted
unique_encrypted:<table>,<field(optional)>
exists_encrypted
exists_encrypted:<table>,<field(optional)>
Custom Local scopes available:
whereEncrypted
whereNotEncrypted
orWhereEncrypted
orWhereNotEncrypted
orWhereEncryptedLike
orderByEncrypted
orderByEncryptedSort
whereInEncrypted
Global scope DecryptSelectScope
automatically booted in models using Encryptable
trait.
Schema::create('users', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
// Once the table has been created, use ALTER TABLE to create VARBINARY
// or BLOB types to store encrypted data.
DB::statement('ALTER TABLE `users` ADD `first_name` VARBINARY(300)');
DB::statement('ALTER TABLE `users` ADD `last_name` VARBINARY(300)');
DB::statement('ALTER TABLE `users` ADD `email` VARBINARY(300)');
DB::statement('ALTER TABLE `users` ADD `telephone` VARBINARY(50)');
The MIT License (MIT). Please see License File for more information.