public function testSaveWithRelations() { // one-to-many, many-to-many $item = factory(App\Post::class)->make(); $post = $item->toArray(); $post['comments'] = []; $post['comments'][] = factory(App\Comment::class)->make()->toArray(); $post['tags'] = []; foreach (App\Tag::all() as $t) { $post['tags'][] = $t['id']; } $item->saveWithRelations($post); $item = App\Post::find(4); self::assertInstanceOf('App\\Post', $item); self::assertEquals('4', $item['id']); $tags = $item->tags; self::assertCount(3, $tags); $comments = $item->comments; self::assertCount(1, $comments); // one-to-one $item = factory(App\Comment::class)->make(); $comment = $item->toArray(); $comment['post'] = factory(App\Post::class)->make()->toArray(); $item->saveWithRelations($comment); $item = App\Comment::find(11); self::assertEquals('11', $item['id']); self::assertEquals(11, App\Comment::count()); $post = $item->post; self::assertEquals('5', $post['id']); self::assertEquals(5, App\Post::count()); // db rollback $this->rollback(); }