定义一个 Mutator

class User extends Model  
{
    public function setPasswordAttribute($password)
    {
        $this->attributes['password'] = bcrypt($password);
    } 
    ... 
}

上面的代码在每次设置密码属性时都会执行 bcrypting

$user = $users->first();
$user->password = 'white rabbit'; //laravel calls mutator on background
$user->save(); // password is bcrypted and one does not need to call bcrypt('white rabbit')