https://github.com/maize-tech/laravel-encryptable
Easily anonymize sensitive data through eloquent queries
https://github.com/maize-tech/laravel-encryptable
api eloquent encryptable encryption laravel php
Last synced: 3 months ago
JSON representation
Easily anonymize sensitive data through eloquent queries
- Host: GitHub
- URL: https://github.com/maize-tech/laravel-encryptable
- Owner: maize-tech
- License: mit
- Created: 2021-06-25T15:09:07.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2025-10-19T14:14:12.000Z (4 months ago)
- Last Synced: 2025-10-19T22:59:48.143Z (4 months ago)
- Topics: api, eloquent, encryptable, encryption, laravel, php
- Language: PHP
- Homepage:
- Size: 369 KB
- Stars: 111
- Watchers: 5
- Forks: 11
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Funding: .github/FUNDING.yml
- License: LICENSE.md
- Security: .github/SECURITY.md
Awesome Lists containing this project
README

# Laravel Encryptable
[](https://packagist.org/packages/maize-tech/laravel-encryptable)
[](https://github.com/maize-tech/laravel-encryptable/actions?query=workflow%3Arun-tests+branch%3Amain)
[](https://github.com/maize-tech/laravel-encryptable/actions?query=workflow%3A"Check+%26+fix+styling"+branch%3Amain)
[](https://packagist.org/packages/maize-tech/laravel-encryptable)
This package allows you to anonymize sensitive data (like the name, surname and email address of a user) similarly to Laravel's Encryption feature, but still have the ability to make direct queries to the database.
An example use case could be the need to make search queries through anonymized attributes.
This package currently supports `MySQL` and `PostgreSQL` databases.
## Installation
You can install the package via composer:
```bash
composer require maize-tech/laravel-encryptable
```
You can publish the config file with:
```bash
php artisan vendor:publish --provider="Maize\Encryptable\EncryptableServiceProvider" --tag="encryptable-config"
```
This is the content of the published config file:
```php
return [
/*
|--------------------------------------------------------------------------
| Encryption key
|--------------------------------------------------------------------------
|
| The key used to encrypt data.
| Once defined, never change it or encrypted data cannot be correctly decrypted.
|
*/
'key' => env('ENCRYPTION_KEY'),
/*
|--------------------------------------------------------------------------
| Encryption cipher
|--------------------------------------------------------------------------
|
| The cipher used to encrypt data.
| Once defined, never change it or encrypted data cannot be correctly decrypted.
| Default value is the cipher algorithm used by default in MySQL.
|
*/
'cipher' => env('ENCRYPTION_CIPHER', 'aes-128-ecb'),
];
```
## Usage
### Basic
To use the package, just add the `Encryptable` cast to all model attributes you want to anonymize.
``` php
Encryptable::class,
'email' => Encryptable::class,
];
}
```
Once done, all values will be encrypted before being stored in the database, and decrypted when querying them via Eloquent.
### Manually encrypt via PHP
``` php
use Maize\Encryptable\Encryption;
$value = "your-decrypted-value";
$encrypted = Encryption::php()->encrypt($value); // returns the encrypted value
```
### Manually decrypt via PHP
``` php
use Maize\Encryptable\Encryption;
$encrypted = "your-encrypted-value";
$value = Encryption::php()->decrypt($value); // returns the decrypted value
```
### Manually decrypt via DB
``` php
use Maize\Encryptable\Encryption;
$encrypted = "your-encrypted-value";
$encryptedQuery = Encryption::db()->encrypt($value); // returns the query used to find the decrypted value
```
### Custom validation rules
You can use one of the two custom rules to check the uniqueness or existence of a given encryptable value.
`ExistsEncrypted` is an extension of Laravel's `Exists` rule, whereas `UniqueEncrypted` is an extension of Laravel's `Unique` rule.
You can use them in the same way as Laravel's base rules:
``` php
use Maize\Encryptable\Rules\ExistsEncrypted;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rule;
$data = [
'email' => 'email@example.com',
];
Validator::make($data, [
'email' => [
'required',
'string',
'email',
new ExistsEncrypted('users'), // checks whether the given email exists in the database
Rule::existsEncrypted('users') // alternative way to invoke the rule
],
]);
```
``` php
use Maize\Encryptable\Rules\UniqueEncrypted;
use Illuminate\Support\Facades\Validator;
$data = [
'email' => 'email@example.com',
];
Validator::make($data, [
'email' => [
'required',
'string',
'email',
new UniqueEncrypted('users'), // checks whether the given email does not already exist in the database
Rule::uniqueEncrypted('users') // alternative way to invoke the rule
],
]);
```
## Testing
```bash
composer test
```
## Changelog
Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.
## Contributing
Please see [CONTRIBUTING](https://github.com/maize-tech/.github/blob/main/CONTRIBUTING.md) for details.
## Security Vulnerabilities
Please review [our security policy](https://github.com/maize-tech/.github/security/policy) on how to report security vulnerabilities.
## Credits
- [Enrico De Lazzari](https://github.com/enricodelazzari)
- [Riccardo Dalla Via](https://github.com/riccardodallavia)
- [All Contributors](../../contributors)
## License
The MIT License (MIT). Please see [License File](LICENSE.md) for more information.