Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

jlguariglia's avatar

Multiple foreign keys referencing to one primary key

Hello guys,

I've been struggling with this for a couple of weeks and I can't seem to find the answer anywhere.

I am trying to reference three foreign keys into one primary key on another table.

Here are some information:

User Table

|---------------------|
|        id           |
|---------------------|
|         1           |
|---------------------|
|         2           |
|---------------------|
|         3           |
|---------------------|

Service Table

Service table with three foreign keys referencing to user_id

|---------------------|------------------|------------------|------------------|
|        id           |     tenant       |  service_person  |     landlord     |
|---------------------|------------------|------------------|------------------|
|         1           |        1         |         2        |         3        |
|---------------------|------------------|------------------|------------------|

User Model

    public function service(){
        return $this->hasMany(Service::class, 'id');
    }

Service Model

    public function tenant(){
        return $this->belongsTo(User::class, 'id', 'tenant');
    }

    public function service_person(){
        return $this->belongsTo(User::class, 'id', 'service_person');
    }

    public function landlord(){
        return $this->belongsTo(User::class, 'id', 'landlord');
    }

So when I try to query with User::find()->service with tinker, only one of my three users find results, which is the service_person. The other ones just return an empty object.

Query result:

>>> User::find(1)->service
=> Illuminate\Database\Eloquent\Collection {#3125
     all: [
       App\Service {#3156
         id: 1,
         tenant: 2,
         service_person: 1,
         landlord: 3
       },
     ],
   }
>>> User::find(2)->service
=> Illuminate\Database\Eloquent\Collection {#3165
     all: [],
   }
>>> User::find(3)->service
=> Illuminate\Database\Eloquent\Collection {#3166
     all: [],
   }

Any idea on how can I achieve that?

Ideally I do not want to do a many to many relationship and create a pivot table.

Thank you in advance

0 likes
7 replies
MichalOravec's avatar

In User model add:

public function tenants() {
    return $this->hasMany(Service::class, 'tenant');
}

public function servicePeople() {
    return $this->hasMany(Service::class, 'service_person');
}

public function landlords() {
    return $this->hasMany(Service::class, 'landlord');
}
1 like
Snapey's avatar

why not just store those three keys directly on the users table?

Then you can use simple belongsTo relationships, referencing the User class and the correct column

1 like
jlguariglia's avatar

Thank you for your answer, @michaloravec. How would that be beneficial for getting all the services related to the authenticated user with auth()->user()->service? What I am trying to achieve is looking for all the services related to that user, doesn't matter if that user is the tenant, service_guy or landlord

jlguariglia's avatar
jlguariglia
OP
Best Answer
Level 1

Follow up on this question, a suggestion from a fellow dev on stack-overflow helped me with the code.

Here's his answer:

"i think the problem is for the User model 's relations:

User Model:


public function servicesTenants(){
return $this->hasMany(Service::class, 'tenant');
}
public function servicesPerson(){
return $this->hasMany(Service::class, 'service_person');
}
public function servicesLandlord(){
return $this->hasMany(Service::class, 'landlord');
}

now, you can try:

$user=User::with(['servicesLandlord','servicesPerson','servicesTenants'])->find($user_id);

if you want to get all user 's services regardless of the relation you can try:

$userServices=Service::where('tenant',$user_id)->orWhere('service_person',$user_id)->
orWhere('landlord',$user_id)->get();
```"
1 like
mariodueso's avatar

Creating a model for the services table and implement HasOneThrough relationship. run 3

Please or to participate in this conversation.