如果数据库为空,代码将不起作用
if the database is empty, code will not work
如果数据库不包含条目,代码将不起作用。如果条目存在,则代码有效。有谁知道为什么代码只有在数据库中已有条目时才有效?
我收到超时错误:
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;
}
问题不在于你的数据库,问题在于如果条件不满足,你永远不会跳出循环。所以 while(true)
将永远 运行。您需要为您的条件添加一个默认情况,这将退出循环。
如果数据库不包含条目,代码将不起作用。如果条目存在,则代码有效。有谁知道为什么代码只有在数据库中已有条目时才有效?
我收到超时错误:
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;
}
问题不在于你的数据库,问题在于如果条件不满足,你永远不会跳出循环。所以 while(true)
将永远 运行。您需要为您的条件添加一个默认情况,这将退出循环。