{"action":"closed","issue":{"url":"https://api.github.com/repos/laravel/framework/issues/20006","repository_url":"https://api.github.com/repos/laravel/framework","labels_url":"https://api.github.com/repos/laravel/framework/issues/20006/labels{/name}","comments_url":"https://api.github.com/repos/laravel/framework/issues/20006/comments","events_url":"https://api.github.com/repos/laravel/framework/issues/20006/events","html_url":"https://github.com/laravel/framework/issues/20006","id":242165197,"number":20006,"title":"Circular reference when loading relations","user":{"login":"abalozz","id":1312336,"avatar_url":"https://avatars2.githubusercontent.com/u/1312336?v=3","gravatar_id":"","url":"https://api.github.com/users/abalozz","html_url":"https://github.com/abalozz","followers_url":"https://api.github.com/users/abalozz/followers","following_url":"https://api.github.com/users/abalozz/following{/other_user}","gists_url":"https://api.github.com/users/abalozz/gists{/gist_id}","starred_url":"https://api.github.com/users/abalozz/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/abalozz/subscriptions","organizations_url":"https://api.github.com/users/abalozz/orgs","repos_url":"https://api.github.com/users/abalozz/repos","events_url":"https://api.github.com/users/abalozz/events{/privacy}","received_events_url":"https://api.github.com/users/abalozz/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":1,"created_at":"2017-07-11T19:56:40Z","updated_at":"2017-07-12T12:41:23Z","closed_at":"2017-07-12T12:41:23Z","body":"- Laravel Version: 5.4\r\n- PHP Version: all\r\n- Database Driver & Version: -\r\n\r\n### Description:\r\nSuppose a post has many comments\r\n```\r\nclass Post extends Model\r\n{\r\n    public function comments()\r\n    {\r\n        return $this->hasMany(Comment::class);\r\n    }\r\n}\r\n```\r\nand a comment belongs to a post\r\n```\r\nclass Comment extends Model\r\n{\r\n    public function post()\r\n    {\r\n        return $this->belongsTo(Post::class);\r\n    }\r\n}\r\n```\r\nYou can get a post with comments doing\r\n```\r\n$post = Post::with('comments')->find($id);\r\n```\r\nbut then, if you iterate comments and use the post of the comment,\r\n```\r\n// Example. Suppose that this can be more complex and you can't simply pass $post to doSomenthingWithComment.\r\nfunction doSomethingWithComment($comment) {\r\n    chechkSomePostCondition($comment->post);\r\n}\r\n\r\nforeach ($post->comments as $comment) {\r\n    doSomenthingWithComment($comment);\r\n}\r\n```\r\nEloquent will make a new query to the database for each `$comment->post`\r\n```\r\nselect * from `posts` where `posts`.`id` = '33' limit 1;\r\nselect * from `posts` where `posts`.`id` = '33' limit 1;\r\nselect * from `posts` where `posts`.`id` = '33' limit 1;\r\n...\r\n```\r\nBut `$comment->post` is the same post that `$post`.\r\n\r\nTo avoid multiple queries you can do\r\n```\r\n$post = Post::with('comments.post')->find($id);\r\n```\r\nbut this still creates an extra query\r\n```\r\nselect * from `posts` where `id` = '33' limit 1; -- Query from Post::find.\r\nselect * from `posts` where `posts`.`id` in ('33'); -- Query from eager loading.\r\n```\r\n\r\nIs it possible for posts and comments to have a circular reference between them to avoid the second query? In this example you can simulate it doing\r\n```\r\nforeach ($post->comments as $comment) {\r\n    $comment->post()->associate($post);\r\n}\r\n```\r\nbut can be this done with Eloquent directly?"}}
{"actor":{"display_login":"themsaid"}}