waseem / laravel-data-encryption
Automatically encryption and decryption data overriding by using getAttribute an setAttribute methods of Eloquent Model.
Requires
- php: ^7.3|^8.0
- ext-openssl: *
- illuminate/database: *
Requires (Dev)
- laravel/legacy-factories: ^1.1
- mockery/mockery: 1.3.1
- orchestra/testbench: ^6.0
- phpunit/phpunit: ^9.0
This package is auto-updated.
Last update: 2025-04-25 22:56:12 UTC
README
A trait to encrypt data models in Laravel, this automatically encrypt and decrypt model data overriding getAttribute an setAttribute methods of Eloquent Model.
How to install
Run composer installation
composer require waseem/laravel-data-encryption
Add ServiceProvider to your app/config.php file
'providers' => [ ... \Waseem\Encipher\EncipherServiceProvider::class, ],
Publish configuration file, this will create config/encrypt.php
php artisan vendor:publish --provider=Waseem\Encipher\EncipherServiceProvider
How to use
-
You must add
SECRET_ENCRYPT_KEY
andENCRYPT_PREFIX
in your .env file or set it in yourconfig/encrypt.php
file -
Use the
Waseem\Encipher\Encipher
trait:use Waseem\Encipher\Encipher;
-
Set the
$encipher
array on your Model.protected $encipher = ['encrypted_property'];
-
Here's a complete example:
<?php namespace App; use Illuminate\Database\Eloquent\Model; use Waseem\Encipher\Encipher; class User extends Model { use Encipher; /** * The attributes that should be encrypted when stored. * * @var array */ protected $encipher = [ 'email', 'name' , 'mobile']; /** * Optionally you can define the attributes that should be converted to camelcase when retrieve. * * @var array */ protected $camelcase = ['name']; }
-
Optional. Encryption your current data
if your lavarel application version 5.8+
If you have current data in your database you can encrypt it with the:
php artisan encipher:encryptionModel 'App\Models\User'
command.Additionally you can decrypt it using the:
php artisan encipher:decryptionModel 'App\Models\User'
command.Note: You must implement first the
Encipher
trait and set$encipher
attributes -
If you are using exists and unique rules with encrypted values replace it with exists_encrypted and unique_encrypted
$validator = validator(['email'=>'wasi@demo.com'], ['email'=>'exists_encrypted:users,email']);
OR
$rules=array( 'email' => 'required|unique_encrypted:users,email' ); $messages=array( "email.unique_encrypted"=>"The email has already been taken." );
-
You can still use
where
functions$validator = User::where('email','wasi@demo.com')->first();
Automatically
wasi@demo.com
will be encrypted and pass it to the query builder.