加密/解密laravel中的数据库字段 [英] Encrypt/Decrypt DB fields in laravel

查看:659
本文介绍了加密/解密laravel中的数据库字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过访问器和变异器对Laravel中的DB字段值进行加密/解密,这在正常的有说服力的事务中可以正常工作.

I am encrypting/decrypting the DB field values in Laravel through accessors and mutators, which is working fine in normal eloquent transactions.

class Person extends Model
{
    use Notifiable;
    protected $table = 'person';

    public function getFirstNameAttribute($value)
    {
        return Crypt::decryptString($value);
    }
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $guarded = array();

    protected function user()
    {
        return $this->belongsTo('App\Models\User', 'useraccount_id', 'id');
    }
}

但是在以下情况下加密和解密不起作用

But the encryption and decryption not working under the following conditions

  1. 雄辩的关系
  2. 数据库原始查询

工作

$person = Person::find($person_id);
$person->firstName;

不起作用

$user = User::find($user_id);
$user->person->firstName;

推荐答案

您可能会在数据库级别进行加密,就好像有人可以访问数据库一样,您不希望他们能够读取人们的医疗数据以纯文本格式.

You would probably have the encryption at the database level, as if someone gets access to the database you don’t want them to be able to read people’s medical data in plain text.

您可以创建一个特征,分别在保存和检索时对数据进行加密和解密:

You could create a trait that encrypts and decrypts data on save and retrieval respectively:

namespace App\Traits;

use Illuminate\Support\Facades\Crypt;
trait Encryptable
{
    /**
     * If the attribute is in the encryptable array
     * then decrypt it.
     *
     * @param  $key
     *
     * @return $value
     */
    public function getAttribute($key)
    {
        $value = parent::getAttribute($key);
        if (in_array($key, $this->encryptable) && $value !== '') {
            $value = decrypt($value);
        }
        return $value;
    }
    /**
     * If the attribute is in the encryptable array
     * then encrypt it.
     *
     * @param $key
     * @param $value
     */
    public function setAttribute($key, $value)
    {
        if (in_array($key, $this->encryptable)) {
            $value = encrypt($value);
        }
        return parent::setAttribute($key, $value);
    }
    /**
     * When need to make sure that we iterate through
     * all the keys.
     *
     * @return array
     */
    public function attributesToArray()
    {
        $attributes = parent::attributesToArray();
        foreach ($this->encryptable as $key) {
            if (isset($attributes[$key])) {
                $attributes[$key] = decrypt($attributes[$key]);
            }
        }
        return $attributes;
    }
}

然后,您可以将特征应用于模型,并定义一个名为$ encryptable的属性,该属性是应加密其数据的列的数组:

You can then just apply the trait to your models, and define a property called $encryptable that’s an array of columns whose data should be encrypted:

class YourModelextends Model
{
    use Encryptable;

    protected $encryptable = [
        'code',
        'keys',
        'allergies'
    ];
}

这篇关于加密/解密laravel中的数据库字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆