class Invitation extends Controller
{

protected $friend_list = array();
protected $level = 1;
//获取好友列表
public function invite_friends_list($my_all_friends = null,$begin  = false)
{
    $uid = \Auth::user()->id;
    if(!$begin && empty($my_all_friends)){
        $my_all_friends = InvitationRelation::select('user_id')->where('fuid' , $uid)->get()->toArray();
        $this->friend_list[$uid] = $my_all_friends;
        array_push($this->friend_list[$uid] , array('level'=>$this->level));
    }
    foreach($my_all_friends as $friend_id) {
        
        if(! empty($friend_id)){
            // Log::info('这是邀请好友的id:',$friend_id);
            $my_all_friends = InvitationRelation::select('user_id')->where('fuid' , $friend_id)->get()->toArray();
            // Log::info('层级:',$my_all_friends);
            if(count($my_all_friends) > 0){
                $this->level++;
                $this->friend_list[$friend_id['user_id']] = $my_all_friends;
                array_push($this->friend_list[$friend_id['user_id']] , array('level'=>$this->level));
                try{
                    $this->invite_friends_list($my_all_friends ,true);
                } catch (\Exception $e) {
                    return \Response::json(['status' => 'error', 'error_msg' => '获取失败'], 422);
                }
            }
            continue;
        }
    }
    return \Response::json($this->friend_list);
}

}

结果:
{

"1": [
    {
        "user_id": 2
    },
    {
        "user_id": 3
    },
    {
        "user_id": 4
    },
    {
        "level": 1
    }
],
"2": [
    {
        "user_id": 5
    },
    {
        "user_id": 6
    },
    {
        "user_id": 7
    },
    {
        "user_id": 8
    },
    {
        "level": 2
    }
],
"7": [
    {
        "user_id": 9
    },
    {
        "user_id": 10
    },
    {
        "user_id": 11
    },
    {
        "level": 3
    }
],
"8": [
    {
        "user_id": 12
    },
    {
        "user_id": 13
    },
    {
        "user_id": 14
    },
    {
        "user_id": 15
    },
    {
        "level": 4
    }
],
"13": [
    {
        "user_id": 20
    },
    {
        "user_id": 21
    },
    {
        "user_id": 22
    },
    {
        "user_id": 23
    },
    {
        "level": 5
    }
],
"15": [
    {
        "user_id": 16
    },
    {
        "user_id": 17
    },
    {
        "user_id": 18
    },
    {
        "user_id": 19
    },
    {
        "level": 6
    }
],
"18": [
    {
        "user_id": 24
    },
    {
        "user_id": 25
    },
    {
        "user_id": 26
    },
    {
        "user_id": 27
    },
    {
        "user_id": 28
    },
    {
        "level": 7
    }
]

}


workliyi
1 声望0 粉丝

会飞的码农