*/ public function parse(string $body): Collection { $text = html_entity_decode(strip_tags($body), ENT_QUOTES, 'UTF-8'); preg_match_all('/(?<=@)[\w]+/', $text, $matches); $names = array_unique($matches[0] ?? []); if (empty($names)) { return collect(); } return $this->resolver->resolveByNames($names)->pluck('id'); } public function syncMentions(Comment $comment): void { $newMentionIds = $this->parse($comment->body); $existingMentionIds = $comment->mentions()->pluck('comment_mentions.user_id'); $addedIds = $newMentionIds->diff($existingMentionIds); $comment->mentions()->sync($newMentionIds->all()); $commenterModel = Config::getCommenterModel(); $addedIds->each(function ($userId) use ($comment, $commenterModel) { $mentionedUser = $commenterModel::find($userId); if ($mentionedUser) { UserMentioned::dispatch($comment, $mentionedUser); } }); } }