如果数据库不包含条目,则该代码将不起作用。如果存在条目,则代码有效。有谁知道为什么只有在数据库中已有条目的情况下代码才起作用?
我收到超时错误:
Maximum execution time of 30 seconds exceeded
_
使用创建用户的代码,然后创建与邀请URL相关的用户个人资料。创建一个长度为7个字符的唯一代码,这是各个邀请URL。 我需要循环,因为必须检查是否已生成代码。还是有更好的解决方案?
protected function create(array $data)
{
if($data['gender'])
{
$avatar = 'defaults\avatars\male.jpg';
}
else
{
$avatar = 'defaults\avatars\female.jpg';
}
if (array_key_exists('team_id', $data) && $data['team_id']){
$team = $data['team_id'];
}else{
$team = Null;
}
if (isset($data['invited_id']) && $data['invited_id']){
$invited_from = $data['invited_id'];
}else{
$invited_from = Null;
}
$user = User::create([
'name' => $data['name'],
'team_id' => $team,
'invited_from_id' => $invited_from,
'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)
]);
$user->profile()->save(new Profile());
while (true) {
$randomstring = substr(str_shuffle("0123456789abcdefghijklmnopqrstuvwxyz"), 0, 7);
if (Invite::where('url','!=', $randomstring)->exists()) {
Invite::create([
'user_id' => $user->id,
'url' => $randomstring
]);
break;
}
}
//store notify for user in database
$usern = User::find($invited_from);
if($usern) {
User::find($usern->id)->notify(new NotifyInvite($user));
}
return $user;
}
答案 0 :(得分:2)
问题不在于您的数据库,问题在于,如果不满足条件,则您永远不会中断循环。因此while(true)
将永远运行。您需要为条件添加默认案例,这将退出循环。