staudenmeir / eloquent-has-many-deep

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

composed relation #186

Closed CrisGuzmanS closed 1 year ago

CrisGuzmanS commented 1 year ago

Hello! ✋ i am not sure if this is the correct place to put this problem, i want to tell you about it to know if you have an approach.

I have this 3 models:

The Organization has a many to many relation with Publication (through organizationpublication table). The Person has a many to many relation with Publication (through personpublication table). The Organization has many to many relation with Person (through organization__person table).

In my Organization model i have my relation between Organization and Publication:

public function publications()
    {
        return $this->belongsToMany(
            Publication::class,
            'organization__publication',
            'organization_id',
            'publication_id'
        );

I want to get deep publications of the organization (the organization publications and the person publication that belongs to the organization). This is why i implemented this method:

public function deepPublications()
    {
        return Publication::whereHas('persons', function ($query) {
            $query->whereOrganization($this);
        })->orWhere(function ($query) {
            $query->whereOrganization($this);
        });
    }

as you see i achieved what i want. The problem is when i want to do this:

Organization::whereHas('deepPublications', function($query){
    //...
});

// and

$organization->deepPublications()->count();

// and

$organization->where('deepPublications','>',3);

as you can see in my deepPublications method i create a new query, i was thinking to create an scope method (scopeDeepPublications), but i am still not achieving what i want. Do you know any approach to do this?

staudenmeir commented 1 year ago

Hi @CrisGuzmanS, You can get a "real" relationships by combining this package with one of my other ones: https://github.com/staudenmeir/laravel-merged-relations

  1. Define the relationships:
class Organization extends Model
{
    use \Staudenmeir\EloquentHasManyDeep\HasRelationships;
    use \Staudenmeir\LaravelMergedRelations\Eloquent\HasMergedRelationships;

    public function deepPublications()
    {
        return $this->mergedRelationWithModel(Publication::class, 'deep_publications');
    }

    public function publications()
    {
        return $this->belongsToMany(
            Publication::class,
            'organization__publication',
            'organization_id',
            'publication_id'
        );
    }

    public function personPublications()
    {
        return $this->hasManyDeep(Publication::class, ['organization__person', Person::class, 'person__publication']);
    }
}
  1. Create the merge view in a migration:
use Staudenmeir\LaravelMergedRelations\Facades\Schema;

Schema:: createOrReplaceMergeView(
    'deep_publications',
    [(new Organization())->publications(), (new Organization())->personPublications()]
);
CrisGuzmanS commented 1 year ago

Wow 🤩. That library is what i need. Thank you so much @staudenmeir

CrisGuzmanS commented 1 year ago

Thank you for all of your amazing contribution 🥇