Muntasir Hasan
Muntasir Hasan

Reputation: 23

Laravel relations within tables

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

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

Upvotes: 0

Views: 49

Answers (1)

Dip Ghosh
Dip Ghosh

Reputation: 585

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

Upvotes: 1

Related Questions