staudenmeir / eloquent-has-many-deep

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

Tag -> belongsToMany -> TagGroup -> belongsToMany -> Tag #75

Closed hasnatbabur closed 4 years ago

hasnatbabur commented 4 years ago

Can anyone help how can I get all related Tags for a Tag which can have multiple attached TagGroups? Thanks

staudenmeir commented 4 years ago

It's possible with a pivot model and a table alias:

class Tag extends Model
{
    use \Staudenmeir\EloquentHasManyDeep\HasRelationships;

    public function tags()
    {
        return $this->hasManyDeep(
            Tag::class,
            [TagTagGroup::class, TagGroup::class, TagTagGroup::class.' as alias']
        );
    }
}

class TagTagGroup extends \Illuminate\Database\Eloquent\Relations\Pivot
{
    use \Staudenmeir\EloquentHasManyDeep\HasTableAlias;
}
hasnatbabur commented 4 years ago

@staudenmeir It worked like a charm. Thanks a lot for the awesome essential library and quick support. You must deserve "World Class Top" developer crown. God bless you.