You can install the package via composer:
composer require oriamdev/encryption-db-fields
1 - First, you need to add the trait EncryptAttributesTrait
to the model when you want to encrypt fields.
use EncryptAttributesTrait;
2 - Define the array with fields to be encrypted
protected array $encrypts = ['name', 'email'];
3 - If you want to search by encrypted field you need to add the array $searchableEncrypts
to the model.
protected array $searchableEncrypts = ['name', 'email'];
NOTES:
- The fields to be encrypted need to be text type.
- For each searchable field you need to add a string field to migration with name
field_signature
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->text('name');
$table->text('email');
$table->string('email_signature',10)->index();
$table->string('name_signature', 10)->index();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
php artisan migrate
The crud operations work the same way and the process to encrypt and decrypt fields are responsibility of the trait EncryptAttributesTrait
The laravel validation rules unique and exists
does not work but, not worry about it.
The package has a Validation Rule Oriamdev\EncryptionUserDbAuth\Rules\Encrypted
The construct need the fully qualified name of the model. User::class or Post::class
After creating the new instance of Encrypted Rule you can pass the unique method
$request->validate([
'email' => [(new Encrypted(User::class))->unique()],
]);
If the validation fails the rule return the default value of validation:unique
After creating the new instance of Encrypted Rule you can pass the exists method
$request->validate([
'email' => [(new Encrypted(User::class))->unique()],
]);
If the validation fails the rule return the default value of validation:unique
$request->validate([
'name' => [(new Encrypted(User::class))->exists()]
]);
$user = User::findByEncrypt('email', '[email protected]');
The method findByEncrypt
return an instance of model or null
if not exists.