Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clarification for how to use with many to many #48

Open
roarkmccolgan opened this issue Feb 5, 2024 · 3 comments
Open

Clarification for how to use with many to many #48

roarkmccolgan opened this issue Feb 5, 2024 · 3 comments

Comments

@roarkmccolgan
Copy link

roarkmccolgan commented Feb 5, 2024

Hi All.

I am returning a collection of orders with their products (many to many) and each order can have the same product.
I would like to know the pivot table data to know how many of the product was for each order.

However in relationships I only get the type "products" and the id.

In the included property, I only get one instance of that product and the pivot data (ordered_qty, received_qty etc) only relates to one of the orders.

I have 2 orders: SPR0004 and SPR0005 they both have a product chocolate croissant, how can I get the ordered qty of them for each order?

{
  "data": [
    {
      "id": "4",
      "type": "orders",
      "attributes": {
        "ref": "SPR00004",
        "status": "dispatched",
        "requested_delivery": "2024-01-25T06:23:40.000000Z",
        "created_at": "2023-07-13T22:23:40.000000Z"
      },
      "relationships": {
        "products": {
          "data": [
            {
              "type": "products",
              "id": "1"
            }
          ]
        },
      }
    },
    {
      "id": "5",
      "type": "orders",
      "attributes": {
        "ref": "SPR00005",
        "status": "dispatched",
        "requested_delivery": "2024-01-25T22:51:09.000000Z",
        "created_at": "2023-07-13T22:51:09.000000Z"
      },
      "relationships": {
        "products": {
          "data": {
            "0": {
              "type": "products",
              "id": "46"
            },
            "2": {
              "type": "products",
              "id": "1"
            }
          }
        },
      }
    }
  ],
  "included": [
    {
      "id": "1",
      "type": "products",
      "attributes": {
        "name": "Chocolate Croissant",
        "slug": "chocolate-croissant",
        "description": null,
        "order_id": 4,
        "ordered_qty": 2,
        "dispatched_qty": 0,
        "delivered_qty": 0
      }
    },
    {
      "id": "46",
      "type": "products",
      "attributes": {
        "name": "Prawn Rissoles",
        "slug": "prawn-rissoles",
        "description": null,
        "order_id": 5,
        "ordered_qty": 2,
        "dispatched_qty": 0,
        "delivered_qty": 0
      }
    }
  ],
  "jsonapi": {
    "version": "1.0"
  }
}

Thank you very much for your assistance!

@lamouize-act
Copy link

Hey, did you find a fix for this one? I have the same issue.

@roarkmccolgan
Copy link
Author

Hi!

Unfortunately not, I ended up using Laravel built in resources very similar in implementation but found it easier to use.
https://laravel.com/docs/11.x/eloquent-resources#generating-resources
Thanks

@Mohammed-Alama
Copy link

@roarkmccolgan so u ends up with not using JsonApiResource for many-to-many relationships ??

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants