staudenmeir / eloquent-has-many-deep

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

not an issue but just asking for a has-many-through relationship between 2 many-to-many relationship #131

Closed ouestdarq closed 3 years ago

ouestdarq commented 3 years ago

Hello every one... I have a table called images and another table called tags... now for the last couple of days I have been looking to make the relationship $image->tags where $image is a Image model connected through an model_has_images table to various other models (Article, Product... etc for example). The table model_has_images contains image_id, model_id and mode;_type as columns... Tags works in the same way (a table named model_has_tags with tag_id, model_id, and model_type columns) ... My idea was to connect the images model to the tags model through a hasManyThrough relation ship with a pivot table but I can't seem to surpass the 3rd level... thus only retrieving a Tag collection through the model_has_images pivot table or a collection of the has_many_tags morphPivot table through the Image model itself... cant break through the 3rd barrier (like otherwise mentioned)... Am I atleast on the right Fing track... lol

ouestdarq commented 3 years ago

is there no model->morphtoMany->model->morphtomany->model?

staudenmeir commented 3 years ago

is there no model->morphtoMany->model->morphtomany->model?

All the possible combinations are supported.

Have you looked into the ability to define a HasManyDeep relationship by concatenating existing relationships? That would be the easiest solution in your case. https://github.com/staudenmeir/eloquent-has-many-deep#existing-relationships

ouestdarq commented 3 years ago
public function tags()
    {
        return $this->hasManyDeep(
            Tag::class,
            [
                Imageable::class, // extends morphPivot with protected $table = 'model_has_images' 
                Taggable::class,  // extends morphPivot with protected $table = 'model_has_tags' 
            ],
            [
                'image_id',
                'model_id',
                'id'
            ],
            [
                'id',
                'model_id',
                'tag_id'
            ]
        )->distinct();
    }

I ended up doing this... don't really know if this is the recommended syntax for my case... but anyhow I'll keep on digging... thnx