【发布时间】:2018-11-17 22:35:42
【问题描述】:
如果数据库不包含条目,则代码将不起作用。如果存在条目,则代码有效。有谁知道为什么代码只有在数据库中已经有条目的情况下才有效?
我收到超时错误:
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;
}
【问题讨论】:
-
你能显示你得到的错误吗?
-
我收到超时错误,我将其添加到我的问题中
-
仅供参考,您不需要重新找到用户来通知他们,因为您已经在
$usern变量中拥有它们,即$usern->notify(new NotifyInvite($user))应该可以正常工作。
标签: database laravel function exists