staudenmeir / eloquent-has-many-deep

Laravel Eloquent HasManyThrough relationships with unlimited levels
MIT License
2.67k stars 157 forks source link

Enable Discussions? #181

Closed iamsubingyawali closed 1 year ago

iamsubingyawali commented 1 year ago

Hi! How about enabling discussion in this repo so that we can use it as a support forum?

I am facing an issue and I need support. Discussions would be more relevant in this case. For now, It would be really helpful if someone can help.

My table structures are:

programs
   - name
activities
   - name
   - program_id
participants
   - name
activity_participant(pivot)
   - activity_id
   - participant_id

With this schema, I want to get all the participants in a certain program. The code I tried is:

public function participants()
{
    return $this->hasManyDeep(Participant::class, [Activity::class, 'activity_participant'])
               ->withPivot('activity_participant');
}

With this code I can get the participants in a certain program without any issues but if a participant is in multiple activities, it returns all those duplicate participant values as well. How do I prevent it?

For example, I have a program P inside which I have two activities A1 and A2. I have two participants P1 and P2 in A1 and only P1 in A2. With this, I will have three total entries in activity_participant pivot table for the activities in that program and above code is returning all three of them. What I wanted is, only two participants entries removing the duplicate P1 participant.

staudenmeir commented 1 year ago

Hi @iamsubingyawali, Good idea, I enabled it.

What database engine and version are you using?

staudenmeir commented 1 year ago

->withPivot('activity_participant')

This is an issue when getting unique participants: Which pivot record do you want to get if a participant is in multiple activities?

Does the pivot table have more columns than activity_id and participant_id?

iamsubingyawali commented 1 year ago

I do not have other columns in the pivot table. I only want to get participants in a program. I am using MariaDB 10.4.27.

If I don't want any values from pivot table, I think this code does the work.

return $this->hasManyDeep(Beneficiary::class, [Activity::class, 'activity_beneficiary']);

But how do I remove duplicate participants?

iamsubingyawali commented 1 year ago

I think below code does the work. Is there any other way to do it?

return $this->hasManyDeep(Beneficiary::class, [Activity::class, 'activity_beneficiary'])->distinct();
staudenmeir commented 1 year ago

I think below code does the work. Is there any other way to do it?

There are other ways, but this is the easiest.

->withPivot('activity_participant')

Do you need to get the pivot record? The issue is that you can't really have both – unique participants and the pivot record. When you only get unique participants, you "throw away" the other pivot records and so the remaining one doesn't really mean anything.

iamsubingyawali commented 1 year ago

No, I don't need any pivot records. I just need to fetch the participant records based on records or relations in the pivot table.

staudenmeir commented 1 year ago

Then ->distinct() is the way to go.

iamsubingyawali commented 1 year ago

Thanks for your help.