whereKey($user->id)->lockForUpdate()->firstOrFail(); if (DB::table('guild_moderator_removals')->where('github_id', $user->github_id)->where('handle', mb_strtolower($handle))->exists()) { throw new GuildModeratorRemoved; } $guild = Guild::query()->createOrFirst(['handle' => mb_strtolower($handle)], [ 'user_id' => $user->id, 'name' => $name, 'description' => $description, 'emoji' => $emoji, ]); if ($guild->wasRecentlyCreated) { if ($icon instanceof UploadedFile) { $guild->update(['icon_path' => $this->images->handle([$icon], directory: 'guild-icons')[0]['path']]); } $this->membership->handle($user, $guild, true); } else { $guild = Guild::query()->whereKey($guild->id)->lockForUpdate()->firstOrFail(); if ($guild->user_id === null) { $guild->update(['user_id' => $user->id]); $this->membership->handle($user, $guild, true); } } return $guild; }); } }