0
我建立在Laravel 5.2一個簡單的用戶註冊和它真的快把我逼瘋了。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\[email protected]');
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']),
]);
}
我不會發表我的看法代碼,我的投入是正確的。
我的問題是,我只能保存一個新的用戶。
發生了什麼事是:
- 我用戶表是空的
- 我填滿我的表單,然後提交它來創建我的第一個用戶
它的工作原理(耶),我正確重定向到我的主頁
我想創建第二個用戶
- 新用戶不存儲在我的表
- 我重定向到主頁然而
如果我在我的表中刪除我的用戶入口(所以這意味着我清空表),那麼我可以再次創建一個新用戶。但我總是隻有一個條目。我無法添加更多。
Route::get('/lol', function() {
return User::create([
'firstname' => 'what',
'lastname' => 'the',
'login' => 'f***',
'organization_id' => 1,
'team_id' => 4,
'password' => 'blabla',
]);
});
,當然,它的工作原理是,每次我打電話的路線魅力/笑
那麼什麼是地獄:
只是出於好奇,我在我的路線文件試過這種打算在這AuthController?我瘋了嗎?
從控制器方法創建第二個用戶時,你得到什麼錯誤? – Qazi