{"action":"opened","issue":{"url":"https://api.github.com/repos/Nanigans/single-table-inheritance/issues/20","repository_url":"https://api.github.com/repos/Nanigans/single-table-inheritance","labels_url":"https://api.github.com/repos/Nanigans/single-table-inheritance/issues/20/labels{/name}","comments_url":"https://api.github.com/repos/Nanigans/single-table-inheritance/issues/20/comments","events_url":"https://api.github.com/repos/Nanigans/single-table-inheritance/issues/20/events","html_url":"https://github.com/Nanigans/single-table-inheritance/issues/20","id":162470326,"number":20,"title":"Loading from relationship fails due to unknown type","user":{"login":"alexwhitman","id":422013,"avatar_url":"https://avatars.githubusercontent.com/u/422013?v=3","gravatar_id":"","url":"https://api.github.com/users/alexwhitman","html_url":"https://github.com/alexwhitman","followers_url":"https://api.github.com/users/alexwhitman/followers","following_url":"https://api.github.com/users/alexwhitman/following{/other_user}","gists_url":"https://api.github.com/users/alexwhitman/gists{/gist_id}","starred_url":"https://api.github.com/users/alexwhitman/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/alexwhitman/subscriptions","organizations_url":"https://api.github.com/users/alexwhitman/orgs","repos_url":"https://api.github.com/users/alexwhitman/repos","events_url":"https://api.github.com/users/alexwhitman/events{/privacy}","received_events_url":"https://api.github.com/users/alexwhitman/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2016-06-27T14:48:28Z","updated_at":"2016-06-27T14:48:28Z","closed_at":null,"body":"Using the following code as an example:\r\n```php\r\nclass Zoo extends Model\r\n{\r\n    public function animals()\r\n    {\r\n        return $this->hasMany(Animal::class);\r\n    }\r\n\r\n}\r\n\r\nclass Animal\r\n{\r\n    use SingleTableInheritanceTrait;\r\n\r\n    protected $table = 'animals';\r\n\r\n    protected static $singleTableTypeField = 'type';\r\n\r\n    protected static $singleTableSubClasses = [ Elephant::class ];\r\n\r\n    public function zoo()\r\n    {\r\n        return $this->belongsTo(Zoo::class);\r\n    }\r\n}\r\n\r\nclass Elephant extends Animal\r\n{\r\n    protected static $singleTableType = 'elephant';\r\n}\r\n```\r\nAssuming a Zoo has been saved with some animals associated, calling `Zoo::with('animals')->get();` fails with `Nanigans\\SingleTableInheritance\\Exceptions\\SingleTableInheritanceException with message 'Cannot construct newFromBuilder for unrecognized type=elephant'`.\r\n\r\nThis is because `get_called_class` at https://github.com/Nanigans/single-table-inheritance/blob/master/src/SingleTableInheritanceTrait.php#L46 returns the base `Animal` class which doesn't know about the `elephant` type.\r\n\r\nA better solution for this would be to define the type in the sub-class map:\r\n```php\r\n    protected static $singleTableSubClasses = [ 'elephant' => Elephant::class ];\r\n```"}}
{"actor":{"display_login":"alexwhitman"}}