Laravel:使用mergeBindings()的复杂查询

I will get the row number if the items are sorted? This query works fine

SELECT @rank:=@rank+1 AS rank,pts from(
    SELECT DISTINCT             
    users.id,
    username,
    SUM(points) AS pts,
    @rank:=@rank+1 AS rank,
    count(users_games.user_id) AS CountGame,
    (SELECT count(user_id) FROM users_games WHERE points=3 AND user_id=users.id ) AS win,
    (SELECT count(user_id) FROM users_games WHERE points=0 AND user_id=users.id ) AS los,
    (SELECT count(user_id) FROM users_games WHERE points=1 AND user_id=users.id ) AS draw
    FROM users_games,users 
    WHERE users_games.user_id=users.id
    AND score_home IS NOT NULL
    AND score_away IS NOT NULL 
    AND points IS NOT NULL 
    GROUP BY users.id
    ORDER BY pts DESC,win DESC,draw DESC,CountGame DESC
    ) t1, (SELECT @rank:=0) t2

but i want use it with Query Builder like this:

 $result= DB::table('users_games')
         ->join('users','users.id','=','users_games.user_id')
         ->select('users.id',
                  'username',
                   DB::raw('SUM(points) as pts'),
                   DB::raw('count(users_games.user_id) as CountGame'),
                   DB::raw('(select count(user_id) from users_games where points=3 and user_id=users.id) as win'),
                   DB::raw('(select count(user_id) from users_games where points=0 and user_id=users.id) as los'),
                   DB::raw('(select count(user_id) from users_games where points=1 and user_id=users.id) as draw'))
         ->whereNotNull('users_games.score_home')
         ->whereNotNull('users_games.score_away')
         ->whereNotNull('users_games.points')
         ->orderBy('pts','desc')
         ->orderBy('win','desc')
         ->orderBy('draw','desc')
         ->orderBy('CountGame','desc')
         ->groupBy('users.id');
         ->get();

I googled and found this function mergeBindings() and my query now looks like this but not working:

 $sub= DB::table('users_games')
         ->join('users','users.id','=','users_games.user_id')
         ->select('users.id',
                  'username',
                   DB::raw('SUM(points) as pts'),
                   DB::raw('count(users_games.user_id) as CountGame'),
                   DB::raw('(select count(user_id) from users_games where points=3 and user_id=users.id) as win'),
                   DB::raw('(select count(user_id) from users_games where points=0 and user_id=users.id) as los'),
                   DB::raw('(select count(user_id) from users_games where points=1 and user_id=users.id) as draw'))
         ->whereNotNull('users_games.score_home')
         ->whereNotNull('users_games.score_away')
         ->whereNotNull('users_games.points')
         ->orderBy('pts','desc')
         ->orderBy('win','desc')
         ->orderBy('draw','desc')
         ->orderBy('CountGame','desc')
         ->groupBy('users.id');

$result=DB::table( DB::raw("({$sub->toSql()}) as sub") )
          ->select(DB::raw('@rank:=@rank+1 AS rank'),'pts')
          ->mergeBindings($sub->getQuery())
          ->get();

how i can solve this Problem?