【问题标题】:Encrypt/Decrypt DB fields in laravel在 laravel 中加密/解密数据库字段
【发布时间】:2018-07-24 22:51:01
【问题描述】:

我正在通过访问器和修改器对 Laravel 中的数据库字段值进行加密/解密,这在正常的 eloquent 事务中运行良好。

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');
    }
}

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

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

工作

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

不工作

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

【问题讨论】:

  • 是的,我试过那个插件,但它也不起作用
  • 你是怎么调用那个方法的,能不能显示代码
  • 工作 ********** $person = Person::find($person_id); $person->名字;不工作 ************** $user = User::find($user_id); $user->person->firstName;
  • 错误是什么?

标签: php laravel encryption laravel-5 eloquent


【解决方案1】:

你可以使用 laravel 的 Crypt Facades 来做到这一点。 请按照这个例子。

use Illuminate\Support\Facades\Crypt;

$encrypted = Crypt::encryptString('Hello world.');

$decrypted = Crypt::decryptString($encrypted);

我从这篇文章链接实现:https://hackthestuff.com/article/laravel6-encryption-and-decryption-model-data-using-crypt-class

【讨论】:

    【解决方案2】:

    根据 Iman 他的回答,我更改了特征,因此它适用于 Laravel 自身的 casts 数组。

    <?php
    namespace App\Library\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 (isset($this->casts[$key]) && $value !== '' && !is_null($value) && $this->casts[$key] == 'encrypt') {
                $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 (isset($this->casts[$key]) && $value !== '' && !is_null($value) && $this->casts[$key] == 'encrypt') {
                $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->casts as $key => $value) {
                if($value == 'encrypt') {
                    if (isset($attributes[$key]) && $attributes[$key] !== '' && !is_null($attributes[$key])) {
                        $attributes[$key] = decrypt($attributes[$key]);
                    }
                }
            }
            return $attributes;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-10
      • 2014-09-02
      • 2012-01-22
      • 2018-02-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多