Encrypt and decrypt Laravel model fields
composer require faustoq/laravel-model-encrypt-fields
Note: The package will be autoregistered thanks to the Laravel Package Auto-Discovery.
Publish the configuration file:
php artisan vendor:publish --provider="ModelEncryptFields\ServiceProvider"
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use ModelEncryptFields\EncryptsAttributes;
class User extends Model
{
// Add EncryptAttributes trait
use EncryptsAttributes;
// List of fields that should be encrypted in your database
protected $encrypts = [
'email',
'name',
];
}
That's it! Now you can automatically encrypt/decrypt the fields specified in the $encrypts
property in your model.
Auto-Encrypt the field name
:
$user->name = "John Doe";
$user->save();
Auto-Decrypt the field name
:
echo "Hello, " . $user->name;