I have the following code in a test:
$post = factory(Post::class)->create();
$first_tag = factory(Tag::class)->create();
$second_tag = factory(Tag::class)->create();
$post->tags()->attach($first_tag);
$post->tags()->attach($second_tag);
$this->assertEquals(2, $post->tags->count());
Until there everything is OK but then I add the following code under the assert line:
$post->tags()->detach($second_tag);
$this->assertEquals(1, $post->tags->count());
This second assert fails and to make it work I have to manually add a line:
$post->tags()->detach($second_tag);
$post->load('tags');
$this->assertEquals(1, $post->tags->count());
Am I missing something? Do I have to manually reload the relationship everytime I detach a model in a belongsToMany relationship? Its really frustrating having to do this. Is there some reason why this is so?