Laravel 5.2 Advanced加入另一个连接条件

时间:2016-05-03 10:00:51

标签: php laravel join laravel-query-builder

所以我正在使用querybuilder构建一个查询,我需要从连接表中计算来自另一个表的where条件的行。我最终无法加入该表,因为这会导致错误的行与此特定连接进行比较。我认为高级连接clausule将允许在其自己的范围中添加另一个连接,但似乎并非如此。

这就是我的尝试:

$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()

所以我的问题是如何加入表格并检查另一个表格中的条件?

0 个答案:

没有答案