Laravel加密时更新方法?

问题描述

嗨,我想在用户创建或编辑数据时对数据库中的某些字段进行加密。如果创建,则加密工作正常,但是当用户编辑数据时,保存在数据库中的值将是未加密的普通文本

<?PHP

namespace App\Traits;
use Crypt;

trait Encryptable
{

     public function toArray()
     {
        $array = parent::toArray();
        foreach ($array as $key => $attribute) {
            if (in_array($key,$this->encryptable) && $array[$key]!='') {
                try {
                $array[$key] = Crypt::decrypt($array[$key]);
               } catch (\Exception $e) {
               }
            }
        }
        return $array;
    }

    public function getAttribute($key)
    {
        try {
            $value = parent::getAttribute($key);
            if (in_array($key,$this->encryptable) && $value!='') {
                $value = Crypt::decrypt($value);
                return $value;
            }
            return $value;
        } catch (\Exception $e) {
            return $value;
        }
    }

    public function setAttribute($key,$value)
    {
        if (in_array($key,$this->encryptable)) {
            $value = Crypt::encrypt($value);
        }
        return parent::setAttribute($key,$value);
    }
}

然后我声明需要在模型中加密哪个字段

<?PHP

namespace App\Model;

use Illuminate\Database\Eloquent\Model;
use App\Traits\Encryptable;

class PraApplication extends Model
{
    use Encryptable;
    protected $table = 'praapplications';
    protected $fillable = ['fullname','icnumber','phone','email'];  

    protected $encryptable = ['icnumber','email'];
}
PHP

用户更新数据时如何使加密起作用?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)