0

i have 4 tables

  1. products
  2. product_relations
  3. orders
  4. order_products

products enter image description here

products_relations enter image description here

orders enter image description here

order_products enter image description here

i tried with Models

  • Order

    public function orderProduct() { return $this->hasMany(OrderProduct::class); }

  • Order Product

    public function product() { return $this->belongsTo(Product::class); }

  • Product

    public function sub_products() { return $this->hasMany(ProductRelation::class, 'parent_product_id', 'id'); }

Expected output to query single order 1011

{
"id": 1011,
"order_type": "DELIVERY",
"order_channel": "ONLINE",
"order_date": "2023-04-01 00:00:00",
"order_product": [
    {
        "id": 3,
        "order_id": 1011,
        "product_id": 1,
        "parent_product_id": null,
        "comment": "this is item comment",
        "product": {
            "id": 1,
            "uuid": "98a8d4fb-5b65-4f62-b121-822924b9549p",
            "barcode": null,
            "type": "ITEM",
            "short_name": "Finasteride",
            "status": 1
        },
        "order_sub_products": [
            {
                "id": 4,
                "order_id": 1011,
                "product_id": 2,
                "parent_product_id": 1,
                "comment": "this is component comment",
                "product": {
                    "id": 2,
                    "type": "COMPONENT",
                    "short_name": "Brand: Milpharm",
                    "status": 1
                }
            }
        ]
    }
]

}

here i have 2 product type (ITEM, COMPONENT) in the same table

  • COMPONENT is sub product of ITEM the order_products table has both ITEM and COMPONENT
  • for ITEM prent_product_id is NULL in the output i need all items and subitems for that order in the list

1 Answers1

1

Check the links below. Hope this is clear your dobuts :

https://stackoverflow.com/questions/25798517/laravel-relations

https://laravel.com/docs/10.x/eloquent-relationships
Dip Ghosh
  • 557
  • 2
  • 16