Laravel 4:Getter 和 Setter 方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23750410/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel 4: Getter and Setter Methods
提问by mwafi
I tried to get Getter & Setter Methods work (from Laravel 3 in Laravel 4)
我试图让 Getter & Setter 方法工作(来自 Laravel 4 中的 Laravel 3)
but display Error.
但显示错误。
is there any work around here?
这里有工作吗?
they are very useful in password case:
它们在密码情况下非常有用:
public function set_password($password)
{
$this->set_attribute('hashed_password', Hash::make($password));
}
回答by mwafi
Just found the answer:
刚刚找到答案:
Now it called Accessors & Mutators
现在它称为访问器和修改器
https://laravel.com/docs/5.2/eloquent-mutators
https://laravel.com/docs/5.2/eloquent-mutators
to hash password before you set it:
在设置之前散列密码:
public function setPasswordAttribute($pass)
{
$this->attributes['password'] = Hash::make($pass);
}