【问题标题】:Eloquent Accessor: update attribute on model instanceEloquent Accessor:更新模型实例的属性
【发布时间】:2021-01-04 22:24:40
【问题描述】:

我想解密我的模型的 email 属性:

protected $fillable = [
    'email',
    'password',
    'remember_token',
    'status',
    'name',
    'lastname',
    'password_changed_at',
    'role',
    'attempts'
];

通过使用这个带有 JSON Append 的 mutator

protected $appends = ['email'];

public function getEmailAttribute($value)
{
    return $this->attributes['email'] === Crypt::decrypt($value);
}

我面临的问题是,当我执行转储并死掉时,响应没有更改电子邮件属性。

关于如何更改响应以返回解密的电子邮件的任何想法? 谢谢:)

【问题讨论】:

    标签: json laravel encryption eloquent accessor


    【解决方案1】:

    不用appends,试试下面:

    public function getEmailAttribute($value)
    {
        return Crypt::decrypt($value);
    }
    

    【讨论】:

    • 你好,这个方法只触发解密后的值,如果你直接访问的话,比如$user->email。但是如果你调用模型的整个实例,属性保持不变。
    • call the whole instance of the model 是什么意思?你能展示你的代码吗?
    • 如果你想使用append,``` protected $appends = ['decrypted_email']; public function getDecryptedEmailAttribute() { return Crypt::decrypt($this->email); } ```
    • 例如,我正在加密登录的电子邮件字段,控制器显然无法正常工作,因为它正在寻找电子邮件的明文并检索用户,显示解密仅启动在刀片中之后,或者您直接调用它,例如 $this->email
    猜你喜欢
    • 1970-01-01
    • 2014-09-27
    • 1970-01-01
    • 2013-11-08
    • 2014-06-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-26
    相关资源
    最近更新 更多