1

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:

  1. 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;
    }
  1. 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));
    }
  1. 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.

  • If I understand it right, when any underage member registers, there should be a legal representative for the underage member. Which means that legal representative must be associated with the underage member. Is it possible in your app that single entity can be legal representative for more than one underage member? – Donkarnash May 29 '22 at 00:18
  • @Donkarnash you understand it right. For underaged member, there is always association with legal representative. And yes, single entity of legal representative can be associated with more than one member. – Matej Dudák May 29 '22 at 16:27
  • In that case the relationship should be between `Member` and `LegalRepresentative`. Also what is a Contact Member - is it - that for each member who is not underage, member himself is contact member? – Donkarnash May 29 '22 at 16:31

0 Answers0