austinheap/laravel-database-encryption
Installation:
composer require austinheap/laravel-database-encryption
Publish the config file:
php artisan vendor:publish --provider="AustinHeap\DatabaseEncryption\DatabaseEncryptionServiceProvider"
Configure Encryption Keys:
Edit config/database-encryption.php and set:
'key' => env('ENCRYPTION_KEY', 'your-32-character-key-here'),
'cipher' => 'AES-256-CBC', // Default, but can be adjusted
First Use Case: Define encrypted attributes in your Eloquent model:
use AustinHeap\DatabaseEncryption\Traits\Encryptable;
class User extends Model
{
use Encryptable;
protected $encryptable = ['ssn', 'credit_card'];
}
Now, ssn and credit_card will be automatically encrypted/decrypted.
Model-Level Encryption:
Encryptable trait in Eloquent models.$encryptable array to specify fields to encrypt.class Patient extends Model
{
use Encryptable;
protected $encryptable = ['medical_history', 'insurance_number'];
}
Dynamic Encryption:
encrypt() and decrypt() methods:
$user = new User();
$user->ssn = $user->encrypt('123-45-6789'); // Manually encrypt
$user->save();
Querying Encrypted Fields:
whereEncrypted() for querying encrypted fields:
$users = User::whereEncrypted('ssn', '123-45-6789')->get();
Mass Assignment:
$fillable:
protected $fillable = ['name', 'ssn', 'credit_card'];
Migrations:
API Responses:
Testing:
$this->app->instance('encrypter', Mockery::mock('overload:\Illuminate\Contracts\Encryption\Encrypter'));
Seeding:
encrypt() in seeders to store encrypted data:
User::create([
'name' => 'John Doe',
'ssn' => (new User)->encrypt('123-45-6789'),
]);
Key Management:
openssl_random_pseudo_bytes(32) to generate a secure key..env (e.g., ENCRYPTION_KEY=your-key-here) and never commit it to version control.Performance Overhead:
description).Query Limitations:
orderBy, groupBy, or raw SQL queries directly. Use whereEncrypted() instead.// ❌ Won't work
User::orderBy('ssn')->get();
// ✅ Works
User::whereEncrypted('ssn', '123-45-6789')->get();
Serialization Issues:
session() or cache()). Exclude them from $guarded or use serialize/unserialize carefully.Database Indexes:
Verify Encryption:
$user = User::find(1);
dd($user->ssn); // Should be decrypted
dd($user->getAttribute('ssn')); // Same as above
Log Encryption Errors:
APP_DEBUG=true) to catch encryption-related exceptions.Check Cipher Configuration:
cipher setting in config/database-encryption.php matches the key length (e.g., AES-256-CBC for 32-byte keys).Clear Cache:
php artisan cache:clear
php artisan config:clear
Custom Encryption Logic:
encrypt() and decrypt() methods in your model:
public function encrypt($value)
{
return parent::encrypt($value . '-custom-suffix');
}
Conditional Encryption:
$encryptable based on conditions (e.g., user role):
protected $encryptable = [];
public function setEncryptableAttribute()
{
if ($this->isAdmin()) {
$this->encryptable = ['api_key'];
}
}
Global Encryption Rules:
class User extends Model
{
protected static function boot()
{
parent::boot();
static::retrieved(function ($user) {
if (!$user->isTrusted()) {
$user->encryptable = ['email']; // Encrypt email for untrusted users
}
});
}
}
Fallback for Missing Key:
try {
$value = $this->decrypt($encryptedValue);
} catch (\Exception $e) {
Log::warning('Encryption key missing for field: ' . $this->getKey());
return $encryptedValue; // Fallback to raw value
}
How can I help you explore Laravel packages today?