{"action":"opened","issue":{"url":"https://api.github.com/repos/laravel/framework/issues/8618","labels_url":"https://api.github.com/repos/laravel/framework/issues/8618/labels{/name}","comments_url":"https://api.github.com/repos/laravel/framework/issues/8618/comments","events_url":"https://api.github.com/repos/laravel/framework/issues/8618/events","html_url":"https://github.com/laravel/framework/issues/8618","id":72682759,"number":8618,"title":"[Proposal] Allow belongsTo association by primary ID","user":{"login":"ghunti","id":392038,"avatar_url":"https://avatars.githubusercontent.com/u/392038?v=3","gravatar_id":"","url":"https://api.github.com/users/ghunti","html_url":"https://github.com/ghunti","followers_url":"https://api.github.com/users/ghunti/followers","following_url":"https://api.github.com/users/ghunti/following{/other_user}","gists_url":"https://api.github.com/users/ghunti/gists{/gist_id}","starred_url":"https://api.github.com/users/ghunti/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ghunti/subscriptions","organizations_url":"https://api.github.com/users/ghunti/orgs","repos_url":"https://api.github.com/users/ghunti/repos","events_url":"https://api.github.com/users/ghunti/events{/privacy}","received_events_url":"https://api.github.com/users/ghunti/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":0,"created_at":"2015-05-02T18:54:48Z","updated_at":"2015-05-02T18:54:48Z","closed_at":null,"body":"Currently to create an association between two models in a (one-to-many) relationship we need to create an instance of the \"one side\" model and pass it to the ```associate()``` method:\r\n```\r\n$comment = new Comment();\r\n$post = Post::find($id);\r\n//do validations\r\n$comment->post()->associate($post);\r\n```\r\nOn the other hand, when creation associations of a \"many-to-many\" relations we need to pass the ids instead of concrete instances of the models:\r\n```\r\n$user = new User();\r\n$user->roles()->attach(1);\r\n//or\r\n$user->roles()->sync(array(1, 2, 3));\r\n```\r\n\r\nMy proposal is to create an ```associateById()``` method to pass the id directly instead of a concrete model.\r\nIf we look at the post/comments example, if we were to have on the validations:\r\n```\r\nreturn array(\r\n            'post' => array(\r\n                'exists:posts,id',\r\n            ),\r\n        );\r\n```\r\nat the Controller we would fetch the post to create a concrete Model and pass it to the ```associate()``` method and we would also check if the Post exists on the database.\r\nIf we don't need to instantiate the Model then only one query would be performed.\r\n\r\nI haven't provided a patch, because ```Illuminate\\Database\\Eloquent\\Relations\\BelongsTo::associate()``` sets the relation on the parent model and I'm not sure where this \"relations\" can be used (besides the ```push()``` method that only makes sense when all the models don't exist yet on the database).\r\n\r\nThanks\r\n"}}