staudenmeir / eloquent-has-many-deep

Laravel Eloquent HasManyThrough relationships with unlimited levels

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Difference from belongs-to-through package?

decadence opened this issue · comments

commented

Hello. Thanks for great and simple to use packages!

I have a question: what is the difference between this and this packages if I need "Belongs to Through" relation? As I see this package works for this case too because you can have BelongsTo in relation chain. Or am I missing something?

For example I have model for item on warehouse. It has product which has category. So with this code I can access product category from WarehouseItem class without this package.

class WarehouseItem extends Model
{
   use HasRelationships;

    /**
     * Product on this warehouse position
     */
    public function product()
    {
        return $this->belongsTo(Product::class, "product_id");
    }

    /*
    * Product category
    */
    public function category()
    {
        return $this->hasOneDeepFromRelations(
            $this->product(),
            (new Product())->category()
        );
    }
}

And simple Product model class

class Product extends Model
{
    public function category()
    {
        return $this->belongsTo(ProductCategory::class, "category_id");
    }
}

And it just works.

Hi @decadence,
This package can do everything (and much more) than belongs-to-through. I took over the maintenance of belongs-to-through from its original creator and I'm just keeping it alive.

commented

Ok I got it. Thanks!