waseem / laravel-data-encryption
Automatically encryption and decryption data overriding by using getAttribute an setAttribute methods of Eloquent Model.
Installs: 51
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 2
Forks: 0
Open Issues: 0
pkg:composer/waseem/laravel-data-encryption
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-11-26 00:30:30 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_KEYandENCRYPT_PREFIXin your .env file or set it in yourconfig/encrypt.phpfile -
Use the
Waseem\Encipher\Enciphertrait:use Waseem\Encipher\Encipher;
-
Set the
$encipherarray 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
Enciphertrait and set$encipherattributes -
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
wherefunctions$validator = User::where('email','wasi@demo.com')->first();
Automatically
wasi@demo.comwill be encrypted and pass it to the query builder.