我想在帖子中找到提到的用户并将其保存在表格中。这里是所有输入的用户名,这些用户名写在@符号后面的帖子中,并在“用户”表中找到,ID 由用户存储。不幸的是,我找不到任何用户,因此找不到用户 ID。如何从用户那里找到 ID?
$msg = I am a post and mention @username1 and @username2
preg_match_all('/@(\w+)/', $msg, $mentionedUsers);
foreach ($mentionedUsers[0] as $mentionedUser) {
$foundUser = User::find($mentionedUser);
if ($foundUser) {
$foundUserId = $foundUser->id;
}
$mentionedUser_save = new Mentioned_post_user;
$mentionedUser_save->user_id_lead = Auth::user()->id;
$mentionedUser_save->user_id = $foundUserId;
$mentionedUser_save->post_id = $post->id;
$mentionedUser_save->save();
}