laravel 4 - 注册后向新用户添加记录

时间:2014-06-26 23:03:00

标签: php laravel laravel-4 registration

我想在重新生成后立即向新用户添加记录,在调用user-> save()之后,我得到了带有null值的user-> id的问题。

这是我的代码

public function register(){

    $input=Input::all();

    $rules=array(
        'username'=>'required|unique:bradoseusers|alphaNum|min:2',
        'email'=> 'required|unique:bradoseusers|email',
        'password'=>'required|alphaNum|min:6'
    );

    $v=Validator::make($input,$rules);

    if($v->passes())
    {   

        $password=$input['password'];
        $password=Hash::make($password);
        $user=new Bradoseusers();
        $user->username=$input['username'];
        $user->Email=$input['email'];
        $user->password=$password;
        $user->save();

        return Redirect::to('login');
    }
    else
    {
        return Redirect::to('/Register')->withErrors($v);
    }

}

2 个答案:

答案 0 :(得分:0)

您可以尝试这样的事情:

public function register()
{
    $input=Input::all();
    $rules=array(...);
    $v = Validator::make($input,$rules);

    if($v->passes()) {   
        $user = new Bradoseusers;
        $user->username = $input['username'];
        $user->Email = $input['email'];
        $user->password = Hash::make($input['password']);
        $user->save();
        // probably you want to redirect to
        // default landing page for logged in user
        return Redirect::to('login'); // i.e /dashboard or /
    }
    else {
        return Redirect::to('/Register')->withErrors($v)->withInput();
    }
}

答案 1 :(得分:0)

问题是我在模型中将primaryKey属性设置为“Email”。因此,它将覆盖主键值,因此您将没有“id”字段。