staudenmeir / eloquent-has-many-deep

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

Difference from belongs-to-through package? #194

Closed decadence closed 1 year ago

decadence commented 1 year ago

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.

staudenmeir commented 1 year ago

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.

decadence commented 1 year ago

Ok I got it. Thanks!