そのため、別のテーブルの where 条件を持つ結合テーブルから行をカウントする必要がある querybuilder を使用してクエリを作成しています。この特定の結合と比較して間違った行が発生するため、最終的にそのテーブルに結合できません。高度な結合句を使用すると、別の結合を独自のスコープに追加できると思いましたが、そうではないようです。
これは私が試したものです:
$profile = DB::table('users')
->select('users.username', 'users.profile_picture', 'users.screen_state',
'count(replies.id) as replies', 'count(interactions.id) as interactions',
'count(alerts.id) as alerts', 'count(user__followers.id) as followers',
'count(user__following.id) as following', 'count(following.id) as volgend')
->leftJoin('replies', function($join)
{
$join->on('users.id', '=', 'replies.user_id')
->leftJoin('alerts')
->on('replies.alert_id', '=', 'alerts.id')
->where('replies.user_id', '=', $user->id)
->where('alerts.hide', '=', 0);
})
->leftJoin('interactions', function($join)
{
$join->on('users.id', '=', 'interactions.user_id')
->leftJoin('alerts')
->on('interactions.alert_id', '=', 'alerts.id')
->where('interactions.user_id', '=', $user->id)
->where('alerts.hide', '=', 0);
})
->leftJoin('alerts', function($join)
{
$join->on('users.id', '=', 'alerts.user_id')
->where('alerts.user_id', '=', $user->id)
->where('alerts.hide', '=', 0);
})
->leftJoin('user__followers as user__following', function($join)
{
$join->on('users.id', '=', 'user__followers.user_id')
->where('user__followers.user_id', '=', $user->id)
->where('user__followers.follower_id', '=', $searchid);
})
->leftJoin('user__followers as following', function($join)
{
$join->on('users.id', '=', 'user__followers.user_id')
->where('user__followers.user_id', '=', $user->id);
})
->leftJoin('user__followers', function($join)
{
$join->on('users.id', '=', 'user__followers.user_id')
->where('user__followers.follower_id', '=', $user->id);
})
->where('users.id', '=', $user->id)
->get();
また、生のSQLでこのクエリを持っているので、私が達成しようとしていることを見ることができます:
SELECT u.username, u.profile_picture, u.screen_state,
(SELECT count(DISTINCT r.id)
FROM `reply` r
LEFT JOIN alerts a
ON r.alert_id = a.alert_content_id
WHERE r.user_id = :userid AND a.hide=0)
AS replies,
(SELECT count(DISTINCT i.id)
FROM `interactions` i
LEFT JOIN alerts a
ON i.alert_id = a.alert_content_id
WHERE i.user_id = :userid AND a.hide=0)
AS interactions,
(SELECT count(DISTINCT a.alerts)
FROM `alerts` a
WHERE a.user_id = :userid AND a.hide=0)
AS alerts,
(SELECT count(DISTINCT uf.id)
FROM `user_followers` uf
WHERE uf.user_id = :userid
AND uf.follower_id = :searchid)
AS following,
(SELECT count(DISTINCT uf.id)
FROM `user_followers` uf
WHERE uf.user_id = :userid)
AS volgend,
(SELECT count(DISTINCT uf.id)
FROM `user_followers` uf
WHERE uf.follower_id = :userid)
AS followers
FROM users u
WHERE id = :userid GROUP BY id
しかし、これは私にエラーを与えます:
Call to undefined method Illuminate\Database\Query\JoinClause::leftJoin()
私の質問は、テーブルに参加して、さらに別のテーブルの状態を確認するにはどうすればよいですか?