{"action":"created","issue":{"url":"https://api.github.com/repos/cloudcreativity/laravel-json-api/issues/58","repository_url":"https://api.github.com/repos/cloudcreativity/laravel-json-api","labels_url":"https://api.github.com/repos/cloudcreativity/laravel-json-api/issues/58/labels{/name}","comments_url":"https://api.github.com/repos/cloudcreativity/laravel-json-api/issues/58/comments","events_url":"https://api.github.com/repos/cloudcreativity/laravel-json-api/issues/58/events","html_url":"https://github.com/cloudcreativity/laravel-json-api/issues/58","id":223598582,"number":58,"title":"How to include Laravel polymoprhic relationship as JSON API relationship?","user":{"login":"SirLamer","id":1343049,"avatar_url":"https://avatars3.githubusercontent.com/u/1343049?v=3","gravatar_id":"","url":"https://api.github.com/users/SirLamer","html_url":"https://github.com/SirLamer","followers_url":"https://api.github.com/users/SirLamer/followers","following_url":"https://api.github.com/users/SirLamer/following{/other_user}","gists_url":"https://api.github.com/users/SirLamer/gists{/gist_id}","starred_url":"https://api.github.com/users/SirLamer/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/SirLamer/subscriptions","organizations_url":"https://api.github.com/users/SirLamer/orgs","repos_url":"https://api.github.com/users/SirLamer/repos","events_url":"https://api.github.com/users/SirLamer/events{/privacy}","received_events_url":"https://api.github.com/users/SirLamer/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":3,"created_at":"2017-04-22T22:57:29Z","updated_at":"2017-04-23T22:08:55Z","closed_at":null,"body":"(Update - I have made progress, check below before wasting time wading through this.)\r\n\r\nWhat is the proper method for declaring a polymorphic relationship? I cannot figure this out and I cannot find an issue discussion on this.\r\n\r\nSo, in my case, I have an \"Address\" model which is used commonly for various assets, example \"user\", \"building\", \"office\", etc. Each of these assets may or may not have an \"address\" relationship object. In Laravel this is managed using its built-in polymorphic system, and each asset has a relationship called \"addressable\".\r\n\r\nIf I try to include the \"address\" data via the relationship on a parent object (ex. \"building\") I get an error like so:\r\n\r\n> [2017-04-22 16:20:20] local.ERROR: CloudCreativity\\JsonApi\\Exceptions\\RuntimeException: Expecting address on App\\Models\\Building\\Building to be a belongs-to relationship. in /home/vagrant/Code/hoistway-cms/vendor/cloudcreativity/laravel-json-api/src/Schema/CreatesEloquentIdentities.php:49\r\n\r\nHere is a lot of code from my project to help with diagnostics, in case I am doing it wrong:\r\n\r\nBuildings\\Request:\r\n```php\r\n<?php\r\n\r\nnamespace App\\JsonApi\\Buildings;\r\n\r\nuse CloudCreativity\\JsonApi\\Http\\Requests\\RequestHandler;\r\n\r\nclass Request extends RequestHandler\r\n{\r\n\r\n    /**\r\n     * @var array\r\n     */\r\n    protected $hasOne = [\r\n        'address'\r\n    ];\r\n\r\n    /**\r\n     * @var array\r\n     */\r\n    protected $hasMany = [\r\n        'devices'\r\n    ];\r\n\r\n    /**\r\n     * @var array\r\n     */\r\n    protected $allowedSortParameters = [\r\n        'name',\r\n        'slug'\r\n    ];\r\n\r\n    /**\r\n     * @var array\r\n     */\r\n    protected $allowedFilteringParameters = [\r\n        'id',\r\n        'name',\r\n        'slug'\r\n    ];\r\n\r\n    /**\r\n     * Request constructor.\r\n     * @param Authorizer $authorizer\r\n     * @param Validators $validator\r\n     */\r\n    public function __construct(Authorizer $authorizer, Validators $validator)\r\n    {\r\n        parent::__construct($authorizer, $validator);\r\n    }\r\n\r\n    /**\r\n     * @return string\r\n     */\r\n    public function getResourceType()\r\n    {\r\n        return Schema::RESOURCE_TYPE;\r\n    }\r\n}\r\n\r\n```\r\n\r\nBuildings\\Schema:\r\n```php\r\n<?php\r\n\r\nnamespace App\\JsonApi\\Buildings;\r\n\r\nuse App\\Models\\Building\\Building;\r\nuse CloudCreativity\\JsonApi\\Exceptions\\RuntimeException;\r\nuse CloudCreativity\\LaravelJsonApi\\Schema\\EloquentSchema;\r\n\r\nclass Schema extends EloquentSchema\r\n{\r\n\r\n    /**\r\n     * @var string\r\n     */\r\n    const RESOURCE_TYPE = 'buildings';\r\n\r\n    /**\r\n     * @var array\r\n     */\r\n    protected $attributes = [\r\n        'name',\r\n        'slug'\r\n    ];\r\n\r\n    /**\r\n     * @return string\r\n     */\r\n    public function getResourceType()\r\n    {\r\n        return self::RESOURCE_TYPE;\r\n    }\r\n\r\n\r\n    /**\r\n     * @param object $resource\r\n     * @param bool $isPrimary\r\n     * @param array $includeRelationships\r\n     * @return array\r\n     */\r\n    public function getRelationships($resource, $isPrimary, array $includeRelationships)\r\n    {\r\n        if (!$resource instanceof Building) {\r\n            throw new RuntimeException('Expecting a Building model.');\r\n        }\r\n\r\n        return [\r\n            'address' => [\r\n                self::SHOW_SELF => true,\r\n                self::SHOW_RELATED => true,\r\n                self::DATA => isset($includeRelationships['address']) ?\r\n                    $resource->address : $this->createBelongsToIdentity($resource, 'address'),\r\n            ],\r\n            'devices' => [\r\n                self::SHOW_SELF => true,\r\n                self::SHOW_RELATED => true,\r\n                self::DATA => function () use ($resource) {\r\n                    return $resource->devices;\r\n                },\r\n            ]\r\n        ];\r\n    }\r\n}\r\n\r\n```\r\n\r\nAddress\\Request:\r\n```php\r\n<?php\r\n\r\nnamespace App\\JsonApi\\Addresses;\r\n\r\nuse CloudCreativity\\JsonApi\\Http\\Requests\\RequestHandler;\r\n\r\nclass Request extends RequestHandler\r\n{\r\n\r\n    /**\r\n     * @var array\r\n     */\r\n    protected $hasOne = [\r\n        'city',\r\n        'country-division',\r\n        'country'\r\n    ];\r\n\r\n    /**\r\n     * @var array\r\n     */\r\n    protected $hasMany = [\r\n        //\r\n    ];\r\n\r\n    /**\r\n     * @var array\r\n     */\r\n    protected $allowedSortParameters = [\r\n        'street',\r\n        'email',\r\n        'latitude',\r\n        'longitude',\r\n    ];\r\n\r\n    /**\r\n     * @var array\r\n     */\r\n    protected $allowedFilteringParameters = [\r\n        'street',\r\n        'street_extra',\r\n        'postal_code',\r\n        'email',\r\n        'phone_mobile',\r\n        'phone_desk',\r\n        'phone_company',\r\n        'phone_fax'\r\n    ];\r\n\r\n    /**\r\n     * Request constructor.\r\n     * @param Authorizer $authorizer\r\n     * @param Validators $validator\r\n     */\r\n    public function __construct(Authorizer $authorizer, Validators $validator)\r\n    {\r\n        parent::__construct($authorizer, $validator);\r\n    }\r\n\r\n    /**\r\n     * @return string\r\n     */\r\n    public function getResourceType()\r\n    {\r\n        return Schema::RESOURCE_TYPE;\r\n    }\r\n}\r\n```\r\n\r\nAddress\\Schema:\r\n```php\r\n<?php\r\n\r\nnamespace App\\JsonApi\\Addresses;\r\n\r\nuse App\\Models\\Address\\Address;\r\nuse CloudCreativity\\JsonApi\\Exceptions\\RuntimeException;\r\nuse CloudCreativity\\LaravelJsonApi\\Schema\\EloquentSchema;\r\n\r\nclass Schema extends EloquentSchema\r\n{\r\n\r\n    /**\r\n     * @var string\r\n     */\r\n    const RESOURCE_TYPE = 'addresses';\r\n\r\n    /**\r\n     * @var array\r\n     */\r\n    protected $attributes = [\r\n        'street',\r\n        'street_extra',\r\n        'postal_code',\r\n        'email',\r\n        'phone_mobile',\r\n        'phone_desk',\r\n        'phone_company',\r\n        'phone_fax',\r\n        'latitude',\r\n        'longitude',\r\n        'place_id',\r\n    ];\r\n\r\n    /**\r\n     * @return string\r\n     */\r\n    public function getResourceType()\r\n    {\r\n        return self::RESOURCE_TYPE;\r\n    }\r\n\r\n\r\n    /**\r\n     * @param object $resource\r\n     * @param bool $isPrimary\r\n     * @param array $includeRelationships\r\n     * @return array\r\n     */\r\n    public function getRelationships($resource, $isPrimary, array $includeRelationships)\r\n    {\r\n        if (!$resource instanceof Address) {\r\n            throw new RuntimeException('Expecting an Address model.');\r\n        }\r\n\r\n        return [\r\n            'city' => [\r\n                self::SHOW_SELF => true,\r\n                self::SHOW_RELATED => true,\r\n                self::SHOW_DATA => true,\r\n                self::DATA => $resource->city,\r\n            ],\r\n            'country' => [\r\n                self::SHOW_SELF => true,\r\n                self::SHOW_RELATED => true,\r\n                self::SHOW_DATA => true,\r\n                self::DATA => $resource->country,\r\n            ],\r\n            'country-division' => [\r\n                self::SHOW_SELF => true,\r\n                self::SHOW_RELATED => true,\r\n                self::SHOW_DATA => true,\r\n                self::DATA => $resource->division,\r\n            ],\r\n            'addressable' => [\r\n                self::SHOW_SELF => true,\r\n                self::SHOW_RELATED => true,\r\n                self::DATA => isset($includeRelationships['addressable']) ?\r\n                    $resource->addressable : $this->createBelongsToIdentity($resource, 'addressable'),\r\n            ]\r\n        ];\r\n    }\r\n}\r\n```\r\n\r\nconfig\\json-api.php (trimmed down for presentation)\r\n```php\r\n<?php\r\n\r\nnamespace App\\Models;\r\n\r\nuse \\App\\JsonApi;\r\n\r\nreturn [\r\n\r\n    ...\r\n    'schemas' => [\r\n        'defaults' => [\r\n            Address\\Address::class => JsonApi\\Addresses\\Schema::class,\r\n            Building\\Building::class => JsonApi\\Buildings\\Schema::class,\r\n            Address\\City::class => JsonApi\\Cities\\Schema::class,\r\n            Address\\Country::class => JsonApi\\Countries\\Schema::class,\r\n            Address\\CountryDivision::class => JsonApi\\CountryDivisions\\Schema::class,\r\n            Building\\Device::class => JsonApi\\Devices\\Schema::class,\r\n            Building\\DeviceType::class => JsonApi\\DeviceTypes\\Schema::class,\r\n        ],\r\n        'v1' => [],\r\n    ],\r\n\r\n    'eloquent-adapter' => [\r\n        'map' => [\r\n            JsonApi\\Addresses\\Schema::RESOURCE_TYPE => Address\\Address::class,\r\n            JsonApi\\Buildings\\Schema::RESOURCE_TYPE => Building\\Building::class,\r\n            JsonApi\\Cities\\Schema::RESOURCE_TYPE => Address\\City::class,\r\n            JsonApi\\Countries\\Schema::RESOURCE_TYPE => Address\\Country::class,\r\n            JsonApi\\CountryDivisions\\Schema::RESOURCE_TYPE => Address\\CountryDivision::class,\r\n            JsonApi\\Devices\\Schema::RESOURCE_TYPE => Building\\Device::class,\r\n            JsonApi\\DeviceTypes\\Schema::RESOURCE_TYPE => Building\\DeviceType::class,\r\n        ],\r\n        'columns' => [],\r\n    ],\r\n];\r\n```"},"comment":{"url":"https://api.github.com/repos/cloudcreativity/laravel-json-api/issues/comments/296492366","html_url":"https://github.com/cloudcreativity/laravel-json-api/issues/58#issuecomment-296492366","issue_url":"https://api.github.com/repos/cloudcreativity/laravel-json-api/issues/58","id":296492366,"user":{"login":"lindyhopchris","id":4464333,"avatar_url":"https://avatars3.githubusercontent.com/u/4464333?v=3","gravatar_id":"","url":"https://api.github.com/users/lindyhopchris","html_url":"https://github.com/lindyhopchris","followers_url":"https://api.github.com/users/lindyhopchris/followers","following_url":"https://api.github.com/users/lindyhopchris/following{/other_user}","gists_url":"https://api.github.com/users/lindyhopchris/gists{/gist_id}","starred_url":"https://api.github.com/users/lindyhopchris/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/lindyhopchris/subscriptions","organizations_url":"https://api.github.com/users/lindyhopchris/orgs","repos_url":"https://api.github.com/users/lindyhopchris/repos","events_url":"https://api.github.com/users/lindyhopchris/events{/privacy}","received_events_url":"https://api.github.com/users/lindyhopchris/received_events","type":"User","site_admin":false},"created_at":"2017-04-23T22:08:55Z","updated_at":"2017-04-23T22:08:55Z","body":"I'm not totally sure I understand the log dump. Isn't that saying that`address_relationship` is a `MorphOne` object?"}}
{"actor":{"display_login":"lindyhopchris"}}