{"action":"created","issue":{"url":"https://api.github.com/repos/laravel/framework/issues/9493","labels_url":"https://api.github.com/repos/laravel/framework/issues/9493/labels{/name}","comments_url":"https://api.github.com/repos/laravel/framework/issues/9493/comments","events_url":"https://api.github.com/repos/laravel/framework/issues/9493/events","html_url":"https://github.com/laravel/framework/pull/9493","id":92458428,"number":9493,"title":"Add $alias to `morphTo` method","user":{"login":"nazieb","id":463775,"avatar_url":"https://avatars.githubusercontent.com/u/463775?v=3","gravatar_id":"","url":"https://api.github.com/users/nazieb","html_url":"https://github.com/nazieb","followers_url":"https://api.github.com/users/nazieb/followers","following_url":"https://api.github.com/users/nazieb/following{/other_user}","gists_url":"https://api.github.com/users/nazieb/gists{/gist_id}","starred_url":"https://api.github.com/users/nazieb/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/nazieb/subscriptions","organizations_url":"https://api.github.com/users/nazieb/orgs","repos_url":"https://api.github.com/users/nazieb/repos","events_url":"https://api.github.com/users/nazieb/events{/privacy}","received_events_url":"https://api.github.com/users/nazieb/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":7,"created_at":"2015-07-01T18:43:24Z","updated_at":"2015-07-02T20:12:53Z","closed_at":null,"pull_request":{"url":"https://api.github.com/repos/laravel/framework/pulls/9493","html_url":"https://github.com/laravel/framework/pull/9493","diff_url":"https://github.com/laravel/framework/pull/9493.diff","patch_url":"https://github.com/laravel/framework/pull/9493.patch"},"body":"This is to deal with Models that have custom `$morphClass`.\r\n\r\n**Background:**\r\nI've encountered the same problem with this: https://github.com/laravel/framework/issues/7347#issuecomment-73520940\r\n\r\nI'm using one to many polymorphic and utilize the custom `$morphClass`. The problem is I can't query the morphed Models.\r\n\r\nTo describe the problem using example:\r\n\r\nThe models:\r\n\r\n```php\r\nclass Photo extends Model\r\n{\r\n    public function imageable()\r\n    {\r\n        return $this->morphTo();\r\n    }\r\n}\r\n\r\nclass Admin extends Model\r\n{\r\n    protected $morphClass = 'admin';\r\n\r\n    public function photos()\r\n    {\r\n        return $this->morphMany('App\\Photo', 'imageable');\r\n    }\r\n}\r\n```\r\n\r\nAnd in the database:\r\n\r\nTable admins\r\n\r\nid | name\r\n---|----\r\n1 | Administrator\r\n\r\nTable photos\r\n\r\nid | url | imageable_type | imageable_id\r\n---|-----|-----------------------|-------------------\r\n1 | http://example.com/image.jpeg | admin | 1\r\n\r\nWhen I'm querying `photos` from `Admin`, it's working fine:\r\n```php\r\n$admin = \\App\\Admin::find(1);\r\nvar_dump($admin->photos); // dump the Photo object\r\n```\r\n\r\nBut when I'm querying the inverse, it's broken:\r\n```php\r\n$photo = \\App\\Photo::find(1);\r\nvar_dump($photo->imageable); // throws exception: Class 'admin' not found\r\n```\r\n\r\nThat's because in `src/Illuminate/Database/Eloquent/Model.php` line 837, the `morphTo` method will initiate whatever value stored in the `imageable_type` column. If the value is the valid class name, it'll work, but since the value is a custom `$morphClass` it will throw the exception.\r\n\r\nAnd since there's no way to lookup a Model's class name from its `$morphClass` value, and using the `AliasLoader` seems like an overkill, I propose to add another parameter to the `morphTo` method. The parameter is simply an array mapping the `$morphClass` to the model's name.\r\n\r\nSo the `Photo` model will be look like this:\r\n```php\r\nclass Photo extends Model\r\n{\r\n    public function imageable()\r\n    {\r\n        $alias = [\r\n            'admin' => \\App\\Admin::class\r\n        ];\r\n        return $this->morphTo('imageable', 'imageable_type', 'imageable_id', $alias);\r\n    }\r\n}\r\n```\r\n\r\nThis way when we're trying to look up the imageable value from a `Photo` it will initiate the correct `Admin` class name"},"comment":{"url":"https://api.github.com/repos/laravel/framework/issues/comments/118153023","html_url":"https://github.com/laravel/framework/pull/9493#issuecomment-118153023","issue_url":"https://api.github.com/repos/laravel/framework/issues/9493","id":118153023,"user":{"login":"taylorotwell","id":463230,"avatar_url":"https://avatars.githubusercontent.com/u/463230?v=3","gravatar_id":"","url":"https://api.github.com/users/taylorotwell","html_url":"https://github.com/taylorotwell","followers_url":"https://api.github.com/users/taylorotwell/followers","following_url":"https://api.github.com/users/taylorotwell/following{/other_user}","gists_url":"https://api.github.com/users/taylorotwell/gists{/gist_id}","starred_url":"https://api.github.com/users/taylorotwell/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/taylorotwell/subscriptions","organizations_url":"https://api.github.com/users/taylorotwell/orgs","repos_url":"https://api.github.com/users/taylorotwell/repos","events_url":"https://api.github.com/users/taylorotwell/events{/privacy}","received_events_url":"https://api.github.com/users/taylorotwell/received_events","type":"User","site_admin":false},"created_at":"2015-07-02T20:12:53Z","updated_at":"2015-07-02T20:12:53Z","body":"Did we figure out this wasn't possible using mutators on the morphing model?\n\nOn Thu, Jul 2, 2015 at 3:06 PM, Tom Schlick <notifications@github.com>\nwrote:\n\n> @taylorotwell <https://github.com/taylorotwell> What I want could be\n> accomplished with this.\n>\n> However I think it would be better to have either a central registry of\n> these classes or when you put a $morphClass = 'something' it would register\n> it for you.\n>\n> Right now it looks like you'd have to both define the $morphClass and then\n> also come up with your own class map to get the one-to-many relation.\n>\n> —\n> Reply to this email directly or view it on GitHub\n> <https://github.com/laravel/framework/pull/9493#issuecomment-118151977>.\n>\n"}}