2 回答

TA贡献1859条经验 获得超6个赞
你没有使用 Eloquent 有什么特别的原因吗?它可能会让事情变得更容易。
由于您已经拥有用户,因此可以这样做。
$user->conversations()->has('users.id', '=', $targetUserId)->first();
(我没有测试过这个解决方案,所以我不确定它是否 100% 有效)
此外,您的第一个查询中可能存在拼写错误。可能是复制粘贴错误,可能是错字。只是确保。
$userConversationIdsArray = DB::table('conversation_user')->where('user_id', $user->id)->pluck('id'); <---- 'id' shouldn't that be 'conversation_id'?

TA贡献1816条经验 获得超4个赞
他们让我走上了正轨。以下方法有效:
/**
* Get a conversation by a target user id.
*
* @param int $targetUserId
* @return mixed
*/
public function getConversationByTargetUserId(int $targetUserId)
{
// Get the current user.
$user = Auth::guard()->user();
// Check the user exists.
if (!$user) {
throw new HttpException(500);
}
return $user->conversations()->whereHas('users', function ($query) use ($targetUserId) {
$query->where('users.id', $targetUserId);
})->first();
}
- 2 回答
- 0 关注
- 117 浏览
添加回答
举报