staudenmeir / eloquent-has-many-deep

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

Help: Usage with: Office -> hasMany -> Employee -> manyToMany Qualification #158

Closed federicocandiago closed 2 years ago

federicocandiago commented 2 years ago

Hello, this might seem like a trivial question, but I can't find any example for this case and despite trying a lot, I don't appear to be able to find a solution.

My tables follow the standard convention:

- offices: id, name..
- employees: id, office_id, name..
- qualifications: id, name..
- employee_qualification: id, employee_id, qualification_id

I need to create a qualifications() function on my Office model so I can retrieve all the qualifications that are represented in the office, unfortunately I can't create a collection as the relationship is needed for the custom pagination I need to input the data in.

I've tried solutions such as:

        return $this->hasManyDeep(Qualification::class, [Employee::class]);
        return $this->hasManyDeep(Qualification::class, 'employee_qualification');

May anyone be so kind to help me? thanks in advance.

staudenmeir commented 2 years ago

Combine both lines:

return $this->hasManyDeep(Qualification::class, [Employee::class, 'employee_qualification']);
federicocandiago commented 2 years ago

Yes!! Thank you so much!