2 years ago

A One-To-Many relationship between an user and other users ?

Posted 2 years ago by starmatt


I am developing a sponsoring system for an app. So when a user registers, he has the possibility to be sponsored by another user, and I am having trouble wrapping my head around how to make such a relationship.

Basically, I have a User model as well as a UserSponsored model. The relationship I am trying to create is as such: A User can have many UserSponsored (beneficiaries) and a UserSponsored can have one User. One To Many right? Except they are really both a Usermodel.

Here's what the users_sponsored table looks like:

    - id
    - sponsor_id // the sponsor user
    - beneficiary_id // the benefiacry user

What I really to do is a relationship where a sponsor User is linked to another beneficiary User by using the above intermediate table.

Is there a way to implement that kinda relationship with Eloquent? I feel like I'm going the wrong way with this... How would you implement such a system?

Thanks a lot for your attention. Matthieu

Please sign in or create an account to participate in this conversation.