cloudcreativity / laravel-json-api

JSON API (jsonapi.org) package for Laravel applications.
http://laravel-json-api.readthedocs.io/en/latest/
Apache License 2.0
780 stars 109 forks source link

Relationships are not returned after POST #511

Closed byjl closed 4 years ago

byjl commented 4 years ago

Sorry if it's poor etiquette to open issues on beta releases, but here's my setup:

I am making a POST request to a resource with many-to-many relationship data included. Records in the post_tags table are being inserted correctly. However the response does not contain the Tags relationship, even if I post to /posts?include=tags

POST:

    {
      "data": {
        "type": "posts",
        "attributes": {
          "name": "foobar"
        },
        "relationships": {
          "tags": {
            "data": [{
              "type": "tags",
              "id": "1"
            }]
          }
        }
      }
    }

Response:

{
    "data": {
        "type": "posts",
        "id": "205",
        "attributes": {
            "name": "foobar",
        },
        "links": {
            "self": "http://xyz.xyz.test/api/v2/posts/205"
        }
    }
}

Requests to GET /posts/205?include=tags work correctly:

{
    "data": {
        "type": "posts",
        "id": "205",
        "attributes": {
            "name": "foobar",
            "tags": [
                {
                    "id": 1,
                    "name": "fizzbuzz",
                    "pivot": {
                        "post_id": 205,
                        "tag_id": 1
                    }
                }
            ]
        },
        "links": {
            "self": "http://xyz.xyz.test/api/v2/posts/205"
        }
    }
}

Is this normal behavior? The documentation makes it seem like the relationship should be returning (under "Supported Query Parameters"): https://laravel-json-api.readthedocs.io/en/latest/crud/creating/

Thank you

byjl commented 4 years ago

I got it working. I did not RTFM 😅

https://laravel-json-api.readthedocs.io/en/latest/fetching/inclusion/ @ Returning Related Resources