首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Laravel 5.2 -登记

Laravel 5.2 -登记
EN

Stack Overflow用户
提问于 2016-05-17 23:58:37
回答 1查看 410关注 0票数 0

我在Laravel5.2上建立了一个简单的用户注册,它真的让我抓狂了。

我从默认的Laravel内置注册系统开始,并添加了一些额外的字段。

我的案件涉及以下文件:

routes.php

代码语言:javascript
运行
复制
// Registration routes...

Route::get('auth/register', function(){
    $organisations = Organization::all();

    return view('auth.register', ['organizations' => $organisations]);
});

Route::post('auth/register', 'Auth\AuthController@postRegister');

User.php

代码语言:javascript
运行
复制
class User extends Model implements AuthenticatableContract,
                                AuthorizableContract,
                                CanResetPasswordContract
{
use Authenticatable, Authorizable, CanResetPassword;

/**
 * The database table used by the model.
 *
 * @var string
 */
protected $table = 'users';

/**
 * The attributes that are mass assignable.
 *
 * @var array
 */
protected $fillable = ['organization_id', 'team_id', 'lastname', 'firstname', 'login', 'password'];

/**
 * The attributes excluded from the model's JSON form.
 *
 * @var array
 */
protected $hidden = ['password', 'remember_token'];

/**
 * Get the user's organisation.
 */
public function organization(){
    return $this->belongsTo('App\Organization');
}

/**
 * Get the user's team.
 */
public function team(){
    return $this->belongsTo('App\Team');
}
}

Organization.php

代码语言:javascript
运行
复制
class Organization extends Model
{
protected $table   = 'organizations';
public $timestamps = false;

protected $fillable = ['name'];

/**
 * Get the authors for the organization.
 */
public function users()
{
    return $this->hasMany('App\Users');
}

/**
 * Get the teams for the organization.
 */
public function teams()
{
    return $this->hasMany('App\Teams');
}

}

Team.php

代码语言:javascript
运行
复制
class Team extends Model
{
protected $table   = 'teams';
public $timestamps = false;

/**
 * Get the team's organisation
 */
public function organisation()
{
    return $this->belongsTo('App\Organisation');
}

/**
 * Get the authors for the team.
 */
public function users()
{
    return $this->hasMany('App\Users');
}

}

AuthController.php

代码语言:javascript
运行
复制
/
/.......
/......
/

/**
 * Get a validator for an incoming registration request.
 *
 * @param  array  $data
 * @return \Illuminate\Contracts\Validation\Validator
 */
protected function validator(array $data)
{
    return Validator::make($data, [
        'firstname'     => 'required|max:255',
        'lastname'      => 'required|max:255',
        'login'         => 'required|max:255',
        'organization'  => 'required|max:255',
        'team'          => 'required|max:255',
        'password'      => 'required|confirmed|min:4',
    ]);
}

/**
 * Create a new user instance after a valid registration.
 *
 * @param  array  $data
 * @return User
 */
protected function create(array $data)
{
    return User::create([
        'firstname'       => $data['firstname'],
        'lastname'        => $data['lastname'],
        'login'           => $data['login'],
        'organization_id' => $data['organization'],
        'team_id'         => $data['team'],
        'password'        => bcrypt($data['password']),
    ]);
}

我不会发布我的视图代码,我的输入是正确的。

我的问题是,我只能存储一个新用户。

正在发生的事情是:

  1. 我的用户表是空的。
  2. 我填写了我的表单,然后提交给我的第一个用户。
  3. 它工作(耶),我被正确地重定向到我的主页。
  4. 我想要创建第二个用户
  5. 新用户没有存储在我的表中
  6. 然而,我被重定向到主页。

如果我删除表中的用户条目(这意味着我将清空表),那么我可以再次创建一个新用户。但我总能有一个,也只有一个条目。我不能再加了。

出于好奇,我在我的路线文件中尝试了这个:

代码语言:javascript
运行
复制
Route::get('/lol', function () {
    return User::create([
        'firstname'       => 'what',
        'lastname'        => 'the',
        'login'           => 'f***',
        'organization_id' => 1,
        'team_id'         => 4,
        'password'        => 'blabla',
    ]);
});

当然,每次我给路线/lol打电话的时候,它就像一种魅力。

那么这个AuthController到底是怎么回事?我疯了吗?

EN

回答 1

Stack Overflow用户

发布于 2016-05-18 08:49:59

好的,问题是,在我注册之后,Laravel自动将用户登录-.- (这其实很不错,但我不知道)。所以现在我的问题解决了。

我回到了Laravel5.1(我当时不到5.2岁),在我看来,认证和注册系统处理得更好。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37288069

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档