This package will automatically encrypt when storing and decrypt when retrieving eloquent using the laravel encryption method. This can be useful when someone illegally downloaded the database and will render the data useless. The salt is user specific so all data cannot be decrypted in just one salt value. Please note that once you lose the salt values the encrypted data cannot be decrypted again, so be careful when using this package.
composer require crisjohn02/encrypter
Create your user table with a salt
column.
Schema::create('users', function(Blueprint $table) {
$table->string('salt');
});
To automatically create salt values, add trait HasSalt
into your User model
<?php
namespace App\YourNameSpace;
use Illuminate\Database\Eloquent\Model;
use Crisjohn02\Encrypter\Traits\HasSalt;
class User extends Model
{
use HasSalt;
}
In your eloquent model, add trait Encryptable
<?php
namespace App\YourNameSpace;
use Illuminate\Database\Eloquent\Model;
use Crisjohn02\Encrypter\Traits\Encryptable;
class Post extends Model
{
use Encryptable;
//Specify the encryptable columns
protected $encryptables = [
'title',
'user_id'
];
}
You can also use the HasUuid
trait and make sure your model has uuid
column
<?php
namespace App\YourNameSpace;
use Illuminate\Database\Eloquent\Model;
use Crisjohn02\Encrypter\Traits\HasUuid;
class Post extends Model
{
use HasUuid;
}
Do not use Encryptable
trait in your user model, this will cause an error when creating new user.