staudenmeir / eloquent-has-many-deep

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

HasManyDeep Relation Issue #219

Closed azaharalam closed 11 months ago

azaharalam commented 11 months ago

While defining relationships between tables, I encountered a challenge in establishing a direct relationship between tables.

The table structure is as follows:

DeliverySchedule(id) DeliveryItem(id, delivery_schedule_id, transaction_id) Transaction(id, order_vehicle_id) OrderVehicle(id, order_id) Order(id) The relationships are defined as follows:

A DeliverySchedule can have more than one DeliveryItem. DeliveryItem belongs to Transaction. Transaction belongs to OrderVehicle. OrderVehicle belongs to Order. I am specifically struggling with creating a direct relationship between DeliverySchedule and Order, as a DeliverySchedule can have more than one Order.

I am reaching out to seek your expert advice and guidance on whether it is possible to achieve this using Laravel's package. Any suggestions or insights you can provide would be immensely beneficial for my project.

Thank you for your time and consideration. I appreciate your assistance.

staudenmeir commented 11 months ago

Hi @azaharalam,

Try this relationship:

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

    public function orders()
    {
        return $this->hasManyDeep(
            Order::class,
            [DeliveryItem::class, Transaction::class, OrderVehicle:class],
            ['delivery_schedule_id', 'id', 'id', 'id'],
            ['id', 'transaction_id', 'order_vehicle_id', 'order_id']
        );
    }
}
azaharalam commented 11 months ago

hi @staudenmeir thank you. it worked for me.