简体   繁体   中英

Multiple foreign keys referencing to one primary key

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: [],
   }

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, so I can show it on the front end.

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

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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM