{"action":"created","issue":{"url":"https://api.github.com/repos/laravel/framework/issues/10501","labels_url":"https://api.github.com/repos/laravel/framework/issues/10501/labels{/name}","comments_url":"https://api.github.com/repos/laravel/framework/issues/10501/comments","events_url":"https://api.github.com/repos/laravel/framework/issues/10501/events","html_url":"https://github.com/laravel/framework/issues/10501","id":109975142,"number":10501,"title":"Polymorphic relation naming is inconsistent","user":{"login":"joostbaptist","id":8181757,"avatar_url":"https://avatars.githubusercontent.com/u/8181757?v=3","gravatar_id":"","url":"https://api.github.com/users/joostbaptist","html_url":"https://github.com/joostbaptist","followers_url":"https://api.github.com/users/joostbaptist/followers","following_url":"https://api.github.com/users/joostbaptist/following{/other_user}","gists_url":"https://api.github.com/users/joostbaptist/gists{/gist_id}","starred_url":"https://api.github.com/users/joostbaptist/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/joostbaptist/subscriptions","organizations_url":"https://api.github.com/users/joostbaptist/orgs","repos_url":"https://api.github.com/users/joostbaptist/repos","events_url":"https://api.github.com/users/joostbaptist/events{/privacy}","received_events_url":"https://api.github.com/users/joostbaptist/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":7,"created_at":"2015-10-06T10:01:49Z","updated_at":"2015-12-07T23:36:59Z","closed_at":null,"body":"Hi,\r\n\r\nLike https://github.com/laravel/framework/issues/9427, I'm running into some strange naming behaviour with polymorphic relations. I would like to ask you to look at this again.\r\n\r\nMy polymorphic relation is defined as follows in class `Module`:\r\n\r\n```\r\npublic function moduleInstance()\r\n{\r\n    return $this->morphTo();\r\n}\r\n```\r\n\r\nNow, in order to load this relation, I have to call:\r\n\r\n```\r\n$module->load('moduleInstance');\r\n```\r\n\r\nHowever, to retrieve this relation, I have to call:\r\n\r\n```\r\n$module->module_instance\r\n```\r\n\r\nThis clearly shows that naming is inconsistent. All relations are named using camel case and it seems that only polymorphic relations are named using snake case. \r\n\r\nHere is another example that clearly shows this inconsistent behaviour:\r\n\r\n```\r\n// moduleType is another, BelongsTo relation that is named using camel case\r\n$module->load(['moduleInstance', 'moduleType']);\r\ndd($module);\r\n```\r\n\r\nBelow is a screenshot that shows the result of executing the above code:\r\n\r\n<img width=\"364\" alt=\"screen shot 2015-10-06 at 11 51 12\" src=\"https://cloud.githubusercontent.com/assets/8181757/10305479/97504596-6c20-11e5-9e97-41748516e2d3.png\">\r\n\r\nAlso note that another empty relation ('moduleInstance') is added using an actually consistent name.\r\n\r\nThanks in advance!"},"comment":{"url":"https://api.github.com/repos/laravel/framework/issues/comments/162705813","html_url":"https://github.com/laravel/framework/issues/10501#issuecomment-162705813","issue_url":"https://api.github.com/repos/laravel/framework/issues/10501","id":162705813,"user":{"login":"joostbaptist","id":8181757,"avatar_url":"https://avatars.githubusercontent.com/u/8181757?v=3","gravatar_id":"","url":"https://api.github.com/users/joostbaptist","html_url":"https://github.com/joostbaptist","followers_url":"https://api.github.com/users/joostbaptist/followers","following_url":"https://api.github.com/users/joostbaptist/following{/other_user}","gists_url":"https://api.github.com/users/joostbaptist/gists{/gist_id}","starred_url":"https://api.github.com/users/joostbaptist/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/joostbaptist/subscriptions","organizations_url":"https://api.github.com/users/joostbaptist/orgs","repos_url":"https://api.github.com/users/joostbaptist/repos","events_url":"https://api.github.com/users/joostbaptist/events{/privacy}","received_events_url":"https://api.github.com/users/joostbaptist/received_events","type":"User","site_admin":false},"created_at":"2015-12-07T23:36:59Z","updated_at":"2015-12-07T23:36:59Z","body":"Hi all, thank you for looking into this. I found that changing the morphTo method in the Model class as shown below fixes the problem for me, although I'm not sure that it won't cause other problems. Thought I'd share this anyway, in case it might be helpful.\r\n\r\n    public function morphTo($name = null, $type = null, $id = null)\r\n    {\r\n        // Get the name of the relation from the function name.\r\n        list($current, $caller) = debug_backtrace(false, 2);\r\n        $relation = Str::camel($caller['function']);\r\n\r\n        // If no name is provided, we will use the name of the relation\r\n        // since that is most likely the name of the polymorphic interface. We can\r\n        // use that to get both the class and foreign key that will be utilized.\r\n        if (is_null($name)) {\r\n            $name = Str::snake($relation);\r\n        }\r\n\r\n        list($type, $id) = $this->getMorphs($name, $type, $id);\r\n\r\n        // If the type value is null it is probably safe to assume we're eager loading\r\n        // the relationship. When that is the case we will pass in a dummy query as\r\n        // there are multiple types in the morph and we can't use single queries.\r\n        if (is_null($class = $this->$type)) {\r\n            return new MorphTo(\r\n                $this->newQuery(), $this, $id, null, $type, $relation\r\n            );\r\n        }\r\n\r\n        // If we are not eager loading the relationship we will essentially treat this\r\n        // as a belongs-to style relationship since morph-to extends that class and\r\n        // we will pass in the appropriate values so that it behaves as expected.\r\n        else {\r\n            $class = $this->getActualClassNameForMorph($class);\r\n\r\n            $instance = new $class;\r\n\r\n            return new MorphTo(\r\n                $instance->newQuery(), $this, $id, $instance->getKeyName(), $type, $relation\r\n            );\r\n        }\r\n    }\r\n\r\n\r\nNote that I use the backtrace to extract the name of the relation (`$relation`, which, in my use case would be 'moduleInstance'), and when instantiating the MorphTo class, this relation name is passed instead of the name of the polymorphic property (`$name`, which would be 'module_instance')."}}