Encrypt Database Values in Laravel with DB Encrypter

Packages

January 31st, 2020

encrypt-security-featured.png

Laravel DB Encrypter is a package by Daniel Częstki that automatically encrypts and decrypts values stored in database fields. It uses Laravel’s standard Encryption via a trait:

1use betterapp\LaravelDbEncrypter\Traits\EncryptableDbAttribute;
2 
3class Client extends Eloquent
4{
5 use EncryptableDbAttribute;
6 
7 /** @var array The attributes that should be encrypted/decrypted */
8 protected $encryptable = [
9 'id_number',
10 'email',
11 ];
12}

Note: encrypted values are longer than their plain text counterparts. The package recommends storing data as a TEXT column:

If your current data is not encrypted, the column will be returned as-is. You can check out the package on GitHub at betterapp/laravel-db-encrypter.

Filed in:

Paul Redmond

Full stack web developer. Author of Lumen Programming Guide and Docker for PHP Developers.