<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
paperscissorsandglue / laravel-encryption-at-rest example snippets
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Paperscissorsandglue\EncryptionAtRest\Encryptable;
class User extends Model
{
use Encryptable;
/**
* The attributes that should be encrypted.
*
* @var array
*/
protected $encryptable = [
'email',
'phone',
'address',
];
}
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Paperscissorsandglue\EncryptionAtRest\EncryptableJson;
class UserProfile extends Model
{
use EncryptableJson;
/**
* The attributes that should have encrypted JSON fields.
*
* @var array
*/
protected $encryptableJson = [
'preferences' => ['notification_email', 'backup_phone'],
'settings' => ['api_key', 'personal_token'],
];
/**
* The attributes that should be cast.
*
* @var array
*/
protected $casts = [
'preferences' => 'json',
'settings' => 'json',
];
}
// Regular attribute access
$email = $user->email; // Automatically decrypted
// Assignment
$user->email = '[email protected]'; // Will be encrypted on save
// Laravel notifications work seamlessly
$user->notify(new WelcomeNotification());
// Form requests and API responses work correctly
return response()->json(['user' => $user]);
// Eloquent serialization works properly
$array = $user->toArray();
// If 'api_key' is encrypted within the preferences JSON
$apiKey = $user->preferences['api_key']; // Automatically decrypted
// Set values that will be encrypted automatically
$user->preferences = [
'api_key' => 'new-secret-key',
'public_setting' => 'not-encrypted'
];
namespace App\Models;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;
use Paperscissorsandglue\EncryptionAtRest\HasEncryptedEmail;
class User extends Authenticatable
{
use HasApiTokens, HasEncryptedEmail, Notifiable;
// ... existing model code
}
use Illuminate\Database\Migrations\Migration;
use Illuminate\Support\Facades\DB;
use App\Models\User;
return new class extends Migration
{
public function up(): void
{
// Rehash all existing user emails
User::all()->each(function ($user) {
$user->handleEmailEncryption();
$user->save();
});
}
};
// Find a user by email
$user = User::findByEmail('[email protected]');
// Or use the scope
$user = User::whereEmail('[email protected]')->first();
use Paperscissorsandglue\EncryptionAtRest\EncryptionService;
public function __construct(EncryptionService $encryptionService)
{
$this->encryptionService = $encryptionService;
}
public function storeData($data)
{
$encryptedData = $this->encryptionService->encrypt($data);
// Store $encryptedData...
}
public function retrieveData($encryptedData)
{
$decryptedData = $this->encryptionService->decrypt($encryptedData);
// Use $decryptedData...
}
use Paperscissorsandglue\EncryptionAtRest\Facades\EncryptionAtRest;
$encrypted = EncryptionAtRest::encrypt('sensitive data');
$decrypted = EncryptionAtRest::decrypt($encrypted);