#StackBounty: #php #mysql #laravel Recursive getting users referral and loop

Bounty: 100

So I’m working with affiliate and doing fine with registration and saving who referred to a user,
now I’m struggling in showing those users with referrals by Level.

Level is not save in the database, I’m thinking of it as incrementing in the logic area?

users’s table structure

id |  name  | sponsor_id |
 1 |  John  |    NULL    |
 2 |  Jane  |      1     |
 3 |  Jess  |      1     |
 4 |  Joe   |      2     |

so it should be like this:

I am John, level 1 are who’s sponsor_id is mine, and level 2 are who’s sponsor_id is id’s of whom I invited.

ID |  Name  |  Level  |       Sponsor       |
1  |  John  |         |   None (first user) |
2  |  Jane  |    1    |   John (1)          |
3  |  Jess  |    1    |   John (1)          |
3  |  Joe   |    2    |   Jane (2)          |

where Jane & Jess‘s sponsor_id is mine, and Joe‘s sponsor_id is Jess

User has sponsor

public function sponsor()
{
    return $this->belongsTo('AppUser', 'sponsor_id');
}

User has referrals

public function referrals()
{
    return $this->hasMany('AppUser', 'sponsor_id');
}


Get this bounty!!!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.