0

我在使用 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) 我还可以看到密码。

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

4

1 回答 1

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);
}
于 2014-07-11T14:48:00.723 回答