I'm working on a project for registration of members to organization. Each registration contains members, and if there is underaged member, it contains also legal representative. Each member may have multiple registrations, and in one registration, multiple members might be registered. Members for legal representative are retrieved through last registration. The same way, I'd like to retrieve legal representative for a member. My problem is, that this does not work with eager loading.
I have 3 models with relationships:
- Member
public function registrations(): BelongsToMany
{
return $this->belongsToMany(Registration::class, 'members_registrations', 'memberId', 'registrationId');
}
public function legalRepresentative(): HasOne
{
$registration = $this->registrations()->latest()->first();
$legalRepresentative = $registration->legalRepresentative();
if ($legalRepresentative == null)
$legalRepresentative = $registration->contactMember();
return $legalRepresentative;
}
- LegalRepresentative
public function registrations(): HasMany
{
return $this->hasMany(Registration::class, 'id', 'legalRepresentativeId');
}
public function members(): BelongsToMany
{
return $this->registrations()->latest()->first()->members()->where('birthDate', '>=', now()->subYears(18));
}
- Registration
public function legalRepresentative(): HasOne
{
return $this->hasOne(LegalRepresentative::class, 'id', 'legalRepresentativeId');
}
public function contactMember(): HasOne
{
return $this->hasOne(Member::class, 'id', 'contactMemberId');
}
public function members(): BelongsToMany
{
return $this->belongsToMany(Member::class, 'members_registrations', 'registrationId', 'memberId');
}
When I run LegalRepresentative::with('members')->get()
, it returns
[
{
"firstName": "Legal",
"lastName": "Representative",
"email": "legal.representative@test.com",
"phone": "+421 000 000 000",
"members": [
{
"firstName": "test2",
"lastName": "test2",
"birthDate": "02.02.2020",
"email": "test2@te.com",
"phone": "+421 000 000 000",
"healthProblems": null,
"dietaryRestrictions": null,
"isDisadvantaged": false,
"hasCard": true,
"formReceived": false,
"name": "test2 test2"
}
]
}
]
However, Member::with('legalRepresentative')->get()
returns
[
{
"firstName": "Test1",
"lastName": "Test1",
"birthDate": "01.10.1999",
"email": "test1@te.com",
"phone": "+421 000 000 000",
"healthProblems": null,
"dietaryRestrictions": null,
"isDisadvantaged": false,
"hasCard": true,
"formReceived": false,
"name": "Test1 Test1",
"legalRepresentative": null
},
{
"firstName": "test2",
"lastName": "test2",
"birthDate": "02.02.2020",
"email": "test2@te.com",
"phone": "+421 000 000 000",
"healthProblems": null,
"dietaryRestrictions": null,
"isDisadvantaged": false,
"hasCard": true,
"formReceived": false,
"name": "test2 test2",
"legalRepresentative": null
}
]
Although Member::latest()->first()->legalRepresentative()->first()
returns
{
"firstName": "Legal",
"lastName": "Representative",
"email": "legal.representative@test.com",
"phone": "+421 000 000 000"
}
This is what my database looks like
Any idea what I might be doing wrong?
Thank you.