Matej Dudák
Matej Dudák

Reputation: 11

Laravel eager loading not working for HasOne relation

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": "[email protected]",
        "phone": "+421 000 000 000",
        "members": [
            {
                "firstName": "test2",
                "lastName": "test2",
                "birthDate": "02.02.2020",
                "email": "[email protected]",
                "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": "[email protected]",
        "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": "[email protected]",
        "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": "[email protected]",
    "phone": "+421 000 000 000"
}

This is what my database looks like

Any idea what I might be doing wrong?

Thank you.

Upvotes: 1

Views: 193

Answers (0)

Related Questions