1. Go to this page and download the library: Download wazza/laravel-db-encryption library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
wazza / laravel-db-encryption example snippets
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Wazza\DbEncrypt\Traits\HasEncryptedAttributes;
class User extends Model
{
use HasEncryptedAttributes;
protected $fillable = ['name', 'email', 'phone'];
/**
* Attributes that should be encrypted.
* These MUST NOT exist as columns in your table!
*/
public array $encryptedProperties = [
'social_security_number',
'credit_card_number',
'medical_record',
];
}
// Create a user with encrypted attributes
$user = User::create([
'name' => 'John Doe',
'email' => '[email protected]',
'social_security_number' => '123-45-6789',
'credit_card_number' => '4111-1111-1111-1111',
]);
// Encrypted attributes work like normal properties
echo $user->social_security_number; // '123-45-6789' (automatically decrypted)
// Update encrypted attributes
$user->social_security_number = '987-65-4321';
$user->save();
// Retrieve and use
$user = User::find(1);
echo $user->social_security_number; // Automatically decrypted!
// Find users by encrypted SSN
$users = User::whereEncrypted('social_security_number', '123-45-6789')->get();
// Find specific user
$user = User::whereEncrypted('credit_card_number', '4111-1111-1111-1111')->first();
// Combine with other queries
$users = User::where('email', 'like', '%@example.com%')
->whereEncrypted('social_security_number', '123-45-6789')
->get();
use Wazza\DbEncrypt\Facades\DbEncrypt;
// Encrypt a value
$encrypted = DbEncrypt::encrypt('sensitive data');
// Decrypt a value
$decrypted = DbEncrypt::decrypt($encrypted);
// Generate search hash
$hash = DbEncrypt::hash('search value');
// Batch operations
$encrypted = DbEncrypt::encryptBatch([
'ssn' => '123-45-6789',
'cc' => '4111-1111-1111-1111',
]);
// Verify without decrypting
$isMatch = DbEncrypt::verify('original text', $encryptedValue);