Reputation: 246
I am in the process of changing the registration process. When the user is invited, the user is assigned to the same team. And if he registers himself, he can choose a team.
Everything works, except with the team_id. I always get the error message:
Undefined index: team_id
What does the error message mean? How can I fix the error?
I do not understand why this does not work, because the same function works with the avatar.
Function:
/**
* 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, [
'name' => 'required|string|max:30',
'username' => 'required|string|max:20|alpha_num|unique:users',
'email' => 'required|string|email|max:255|unique:users',
'password' => 'required|string|min:6',
'birthday' => 'required|date|before_or_equal:-16 years',
'agb' => 'accepted',
'gender' => 'required|boolean',
'team_id' => 'numeric'
]);
}
/**
* Create a new user instance after a valid registration.
*
* @param array $data
* @return \App\User
*/
protected function create(array $data)
{
if($data['gender'])
{
$avatar = 'defaults\avatars\male.jpg';
}
else
{
$avatar = 'defaults\avatars\female.jpg';
}
if($data['team_id'])
{
$team = $data['team_id'];
}
else
{
$team = Null;
}
$user = User::create([
'name' => $data['name'],
'team_id' => $team,
'username' => $data['username'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
'birthday' => $data['birthday'],
'gender' => $data['gender'],
'slug' => str_slug($data['username']),
'avatar' => $avatar,
'active' => false,
'activation_token' => str_random(255)
]);
Profile::create(['user_id' => $user->id ]);
while (true) {
$randomstring = substr(str_shuffle("0123456789abcdefghijklmnopqrstuvwxyz"), 0, 7);
if (Invite::where('url','!=', $randomstring)->exists()) {
Invite::create([
'user_id' => $user->id,
'url' => $randomstring
]);
break;
}
}
return $user;
}
Upvotes: 0
Views: 1121
Reputation: 10254
Read the error and you will be able to understand what it means.
See this line:
if($data['team_id'])
What if the $data
array just don't contains the team_id
index? This code is not valid so. If you want to verify it, you must use use array_key_exists()
if (array_key_exists('team_id', $key) && $data['team_id'])
or
if (isset($key['team_id']) && $data['team_id'])
Both are safe ways to do what you want.
Upvotes: 1