继续说密码字段是必需的

时间:2014-07-11 13:21:45

标签: php hash laravel passwords ardent

我在使用laravel创建用户时遇到问题。它继续说"密码字段是必需的"。我检查了帖子并填写了密码。我使用ardent并在我的用户模型的开头使用它:

public static $passwordAttributes  = array('password');
public $autoHashPasswordAttributes = true;

以及后来:

function setpasswordAttribute($value)
{
    $this->attributes['password'] = Hash::make($value);
}

这需要自动哈希密码。

这是处理帖子的用户控制器部分:

public function createUserPost()
{
    var_dump(Input::all());
    $user = new User(Input::except('locations'));
    var_dump($user);

    $user->organisation_id = Auth::user()->organisation_id;

    // get locations
    $input_locations = Input::only('locations');

    if($user->save())
    {
        // Match the id's of location to a new user
        $new_user_id = $user->id;
        foreach ($input_locations as $key => $value) {
            $location = Location::find($value);
            User::find($new_user_id)->locations()->attach($location);
        }
        return Redirect::to('users/'.$user->id);
    }
    return $this->createUser($user)->withErrors($user);
}

在var_dump($ user)我也可以看到密码。

真的希望有人可以帮助我,因为我真的被困在这里。如果我需要提供更多信息,我很乐意给出一些信息。

1 个答案:

答案 0 :(得分:0)

这为我解决了。唯一的问题是附加方法还没有工作。

public function createUserPost()
{
    $user = new User(Input::except('locations','password'));
    $password = Input::only('password');
    $hashed_password = $password['password'];
    $user->password = $hashed_password;

    $user->organisation_id = Auth::user()->organisation_id;

    // pak het field locations
    $input_locations = Input::only('locations');

    if($user->save())
    {
        // Koppel de id's van de locatie aan de nieuwe user
        $new_user_id = $user->id;
        foreach ($input_locations as $key => $value) {
            $location = Location::find($value);
            User::find($new_user_id)->Location()->attach($location);
        }
        return Redirect::to('users/'.$user->id);
    }
    return $this->createUser($user)->withErrors($user);
}
相关问题