staudenmeir / eloquent-has-many-deep

Laravel Eloquent HasManyThrough relationships with unlimited levels

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

need help with hasManyDeep with belongsTo

mrvnklm opened this issue · comments

I've been wrapping my head around this for 1h+ now, but I am not even sure anymore, if I am right using hasManyDeep and as the example uses a morph to relation I maybe got even a bit more confused, maybe you can help me out here?

I want to get all Locations for an Order and the relationships look like this:
Order -> hasMany -> OrderItem -> hasMany -> Appointment -> hasOne (actually <-belongsTo) Location

Hi @mrvnklm,
Try this relationship:

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

    public function locations()
    {
        return $this->hasManyDeep(
            Location::class,
            [OrderItem::class, Appointment::class],
            [null, null, 'id'],
            [null, null, 'location_id']
        );
    }
}

It works, now it seems so obvious and simple.
Thanks a lot, enjoy the weekend!