1. Go to this page and download the library: Download hamoi1/eloquent-encryptable 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/ */
hamoi1 / eloquent-encryptable example snippets
return [
/*
|--------------------------------------------------------------------------
| Hill Cipher Key Matrix
|--------------------------------------------------------------------------
|
| The key matrix for the Hill cipher encryption. Must be a square matrix
| (2x2 or 3x3) and invertible. The matrix should be provided as a JSON string.
|
*/
'key' => '[[3,2],[5,7]]', // 2x2 matrix example
/*
|--------------------------------------------------------------------------
| Previous Key Matrix
|--------------------------------------------------------------------------
|
| Used for key rotation. Store your previous key here when updating
| the main key to allow re-encryption of existing data.
|
*/
'previous_key' => null,
/*
|--------------------------------------------------------------------------
| Models to Re-encrypt
|--------------------------------------------------------------------------
|
| List of model classes that should be processed during key rotation.
|
*/
'models' => [
// App\Models\User::class,
// App\Models\Customer::class,
],
];
namespace App\Models;
use Hamoi1\EloquentEncryptAble\Traits\EncryptAble;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
use EncryptAble;
protected $fillable = [
'name', 'email', 'phone', 'address'
];
/**
* The attributes that should be encrypted.
*
* @var array
*/
protected $encryptAble = [
'phone', 'address'
];
}
// Create a new user - phone and address will be encrypted automatically
$user = User::create([
'name' => 'John Doe',
'email' => '[email protected]',
'phone' => '+1234567890',
'address' => '123 Main Street'
]);
// Retrieve user - phone and address will be decrypted automatically
$user = User::find(1);
echo $user->phone; // +1234567890 (decrypted)
echo $user->address; // 123 Main Street (decrypted)
use Hamoi1\EloquentEncryptAble\Rules\EncryptAbleUniqueRule;
public function rules()
{
return [
'phone' => [
'mail' => [
' ],
];
}
use Hamoi1\EloquentEncryptAble\Rules\EncryptAbleExistRule;
public function rules()
{
return [
'parent_phone' => [
'
use Hamoi1\EloquentEncryptAble\Services\EloquentEncryptAbleService;
$service = app(EloquentEncryptAbleService::class);
// Encrypt a string
$encrypted = $service->encrypt('sensitive data');
// Decrypt a string
$decrypted = $service->decrypt($encrypted);
// Decrypt using previous key (for key rotation)
$decrypted = $service->decrypt($encrypted, true);
// This will throw an exception with a suggested matrix if current key is invalid
try {
$service = app(EloquentEncryptAbleService::class);
$service->encrypt('test');
} catch (InvalidArgumentException $e) {
echo $e->getMessage(); // Contains the suggested matrix
}
protected static function bootEncryptAble()
{
parent::bootEncryptAble();
if (config('app.debug')) {
\Log::info('Encrypting model: ' . static::class);
}
}