Laravel-json-api: How do I create an address record for a client (polymorphic)?

Created on 22 Nov 2019  路  3Comments  路  Source: cloudcreativity/laravel-json-api

Clients have a morphMany() relationship to Addresses.

Route:
{{host}}/api/v1/addresses

Body:
{
"data" : {
"type": "addresses",
"attributes": {

    },
    "relationships": {
        "clients": {
            "data": {
                    "type": "clients",
                    "id": "2"
            }
        }
    }
}

}

Error:
Cannot insert the value NULL into column 'addressable_type', table 'OmegaMCS.dbo.addresses'; column does not allow nulls. INSERT fails.

question

Most helpful comment

Here's what was missing.

On the JSON API Address Adapter:

protected function addressable() {
    return $this->belongsTo();
}

In the Address request:

"relationships": {
            "addressable": {
                "data": {
                        "type": "clients",
                        "id": "2"
                }
            }
        }

All 3 comments

Here's what was missing.

On the JSON API Address Adapter:

protected function addressable() {
    return $this->belongsTo();
}

In the Address request:

"relationships": {
            "addressable": {
                "data": {
                        "type": "clients",
                        "id": "2"
                }
            }
        }

@dobbie004 good to see that you solved it yourself. Relationships (especially poly ones) can be quite a pain in the neck. :sweat_smile:

Feel free to close the issue if it has been resolved. :v:

Yes sorry I didn't have a chance to reply over the weekend, but glad to hear that you found the missing piece to get it working!!

Was this page helpful?
0 / 5 - 0 ratings