{"action":"created","issue":{"url":"https://api.github.com/repos/laravel/framework/issues/23203","repository_url":"https://api.github.com/repos/laravel/framework","labels_url":"https://api.github.com/repos/laravel/framework/issues/23203/labels{/name}","comments_url":"https://api.github.com/repos/laravel/framework/issues/23203/comments","events_url":"https://api.github.com/repos/laravel/framework/issues/23203/events","html_url":"https://github.com/laravel/framework/pull/23203","id":298013940,"number":23203,"title":"[5.6] Enable relationship unsetting ","user":{"login":"jerguslejko","id":4470539,"avatar_url":"https://avatars2.githubusercontent.com/u/4470539?v=4","gravatar_id":"","url":"https://api.github.com/users/jerguslejko","html_url":"https://github.com/jerguslejko","followers_url":"https://api.github.com/users/jerguslejko/followers","following_url":"https://api.github.com/users/jerguslejko/following{/other_user}","gists_url":"https://api.github.com/users/jerguslejko/gists{/gist_id}","starred_url":"https://api.github.com/users/jerguslejko/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jerguslejko/subscriptions","organizations_url":"https://api.github.com/users/jerguslejko/orgs","repos_url":"https://api.github.com/users/jerguslejko/repos","events_url":"https://api.github.com/users/jerguslejko/events{/privacy}","received_events_url":"https://api.github.com/users/jerguslejko/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":2,"created_at":"2018-02-17T15:49:40Z","updated_at":"2018-02-17T18:15:01Z","closed_at":"2018-02-17T18:15:01Z","author_association":"CONTRIBUTOR","pull_request":{"url":"https://api.github.com/repos/laravel/framework/pulls/23203","html_url":"https://github.com/laravel/framework/pull/23203","diff_url":"https://github.com/laravel/framework/pull/23203.diff","patch_url":"https://github.com/laravel/framework/pull/23203.patch"},"body":"This PR introduces a conveniece method for _unsetting_ already loaded relationships.\r\n\r\nConsider the following: You have a `User` class which has `articles` relationship. You may want to provide an `addArticle()` method on the User class.\r\n\r\n```php\r\nclass User extends Model\r\n{\r\n    public function articles()\r\n    {\r\n        return $this->hasMany(Article::class);\r\n    }\r\n\r\n    public functiona addArticle($attributes)\r\n    {\r\n        return $this->articles()->create($attributes);\r\n    }\r\n}\r\n```\r\n\r\nThen, your tests would look like this:\r\n\r\n```php\r\n/** @test */\r\npublic function it_creates_new_article()\r\n{\r\n    $user = factory(User::class)->create();\r\n\r\n    $this->assertCount(0, $user->articles);\r\n\r\n    $user->addArticle([\r\n        'title' => 'Frying pan.',\r\n    ]);\r\n\r\n    // This assertion would fail, as the `articles` relationship\r\n    // has already been loaded and is cached.\r\n    //\r\n    // In order to make it pass, we would need to either refresh\r\n    // the user model, or query the articles relationship manually.\r\n    $this->assertCount(1, $user->articles);\r\n}\r\n```\r\n\r\nThe solution this PR proposes is to provide a `Model::unsetRelation()` method to enable _\"forgetting\"_ already loaded relationships.\r\n\r\n```php\r\nclass User extends Model\r\n{\r\n    public function addArticle($attributes)\r\n    {\r\n        $article = $this->articles()->create($attribute);\r\n\r\n        $this->unsetRelation('articles');\r\n\r\n        return $article;\r\n    }\r\n}\r\n```\r\n\r\nThen the test would pass without having to refresh the relationship manually.\r\n\r\nBy using this approach, we hide the complexity of re-fetching the relationships manually inside convenience methods like `addArticle()`. An alternative approach would be to reload the relationship inside the `addArticle()` method but that would fire off one more query, even when not necessary.\r\n\r\n---\r\n\r\nThe question now is wether you would consider aliasing this method to `$model->forget()` as that reads a bit nicer."},"comment":{"url":"https://api.github.com/repos/laravel/framework/issues/comments/366460488","html_url":"https://github.com/laravel/framework/pull/23203#issuecomment-366460488","issue_url":"https://api.github.com/repos/laravel/framework/issues/23203","id":366460488,"user":{"login":"jerguslejko","id":4470539,"avatar_url":"https://avatars2.githubusercontent.com/u/4470539?v=4","gravatar_id":"","url":"https://api.github.com/users/jerguslejko","html_url":"https://github.com/jerguslejko","followers_url":"https://api.github.com/users/jerguslejko/followers","following_url":"https://api.github.com/users/jerguslejko/following{/other_user}","gists_url":"https://api.github.com/users/jerguslejko/gists{/gist_id}","starred_url":"https://api.github.com/users/jerguslejko/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jerguslejko/subscriptions","organizations_url":"https://api.github.com/users/jerguslejko/orgs","repos_url":"https://api.github.com/users/jerguslejko/repos","events_url":"https://api.github.com/users/jerguslejko/events{/privacy}","received_events_url":"https://api.github.com/users/jerguslejko/received_events","type":"User","site_admin":false},"created_at":"2018-02-17T18:15:01Z","updated_at":"2018-02-17T18:15:01Z","author_association":"CONTRIBUTOR","body":"@kimuliSiraje thanks but the point of the PR is to not have call `->fresh()` manually.\r\n\r\n@mfn that actually works perfectly. the api is not as pretty (`unset($user->articles)`) but it's good enough! thanks"}}
{"actor":{"display_login":"jerguslejko"}}