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
=> IlluminateDatabaseEloquentCollection {#3125
all: [
AppService {#3156
id: 1,
tenant: 2,
service_person: 1,
landlord: 3
},
],
}
>>> User::find(2)->service
=> IlluminateDatabaseEloquentCollection {#3165
all: [],
}
>>> User::find(3)->service
=> IlluminateDatabaseEloquentCollection {#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

from Newest questions tagged laravel-5 – Stack Overflow https://ift.tt/3cGMNpI
via IFTTT

Related Posts

Codeigniter : Parse error: syntax error, unexpected ‘const’ (T_CONST), expecting variable (T_VARIABLE) in Laravel project

I’m getting following error: **Parse error: syntax error, unexpected ‘const’ (T_CONST), expecting variable (T_VARIABLE)** Note : It’s working in local but facing issue in production server. private…

Firebase receive notification while tab is active or on focus

What i want is to be able to perform an action when a user receives a notification while the browser is open and tab is active or…

Laravel’s alias loader does not find class

We have a legacy project that we cannot update and we need to make some changes in symfony’s Response.php in vendor. We have solved this by copying…

Laravel 5 – generic document management

I have a system where you can create different types of unique documents. For instance, one document is called Project Identified and this expects certain inputs. Originally,…

Laravel Nova limit the results in indexQuery

I ran intro a situation where I need to limit the results of a resource to only 3 results. To be more specific, based on the logged…

Auditoria en laravel 5.8 [closed]

Cómo puedo automatizar el registro de actividades de un usuario en laravel? Si un usuario ingresa a un app de laravel, debo guardar toda su actividas, a…

Leave a Reply

Your email address will not be published.