我正在Laravel 5.2上构建一个简单的用户注册,这真的让我发疯。
我从默认的Laravel内置注册系统开始,并添加了一些额外的字段。
我的案例涉及以下文件:
routes.php文件
// 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的
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
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
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
/
/.......
/......
/
/**
* 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']),
]);
}
我不会发布我的观看代码,我的输入是正确的。
我的问题是我只能存储一个新用户。
发生的事情是:
它有效(yay),我被正确地重定向到我的主页
我想创建第二个用户
如果我删除了我的表中的用户条目(这意味着我清空了我的表),那么我可以再次创建一个新用户。但我总能拥有一个且只有一个条目。我不能添加更多。
出于好奇,我在路线文件中尝试了这个:
Route::get('/lol', function () {
return User::create([
'firstname' => 'what',
'lastname' => 'the',
'login' => 'f***',
'organization_id' => 1,
'team_id' => 4,
'password' => 'blabla',
]);
});
当然,每当我调用路线 / lol 时,它就像魅力一样
那么 AuthController 到底发生了什么?我不在乎吗?
答案 0 :(得分:0)
好的问题是,在我注册后,Laravel会自动将用户登录为-.-(实际上这很不错,但我不知道)。所以现在我的问题已经解决了。
我回到Laravel 5.1(我在5.2以下),在我看来,验证和注册系统处理得更好。