public function testBelongsToMany()
 {
     $user = User::create(array('name' => 'John Doe'));
     // Add 2 clients
     $user->clients()->save(new Client(array('name' => 'Pork Pies Ltd.')));
     $user->clients()->create(array('name' => 'Buffet Bar Inc.'));
     // Refetch
     $user = User::with('clients')->find($user->_id);
     $client = Client::with('users')->first();
     // Check for relation attributes
     $this->assertTrue(array_key_exists('user_ids', $client->getAttributes()));
     $this->assertTrue(array_key_exists('client_ids', $user->getAttributes()));
     $clients = $user->getRelation('clients');
     $users = $client->getRelation('users');
     $this->assertInstanceOf('Illuminate\\Database\\Eloquent\\Collection', $users);
     $this->assertInstanceOf('Illuminate\\Database\\Eloquent\\Collection', $clients);
     $this->assertInstanceOf('Client', $clients[0]);
     $this->assertInstanceOf('User', $users[0]);
     $this->assertCount(2, $user->clients);
     $this->assertCount(1, $client->users);
     // Now create a new user to an existing client
     $user = $client->users()->create(array('name' => 'Jane Doe'));
     $this->assertInstanceOf('Illuminate\\Database\\Eloquent\\Collection', $user->clients);
     $this->assertInstanceOf('Client', $user->clients->first());
     $this->assertCount(1, $user->clients);
     // Get user and unattached client
     $user = User::where('name', '=', 'Jane Doe')->first();
     $client = Client::Where('name', '=', 'Buffet Bar Inc.')->first();
     // Check the models are what they should be
     $this->assertInstanceOf('Client', $client);
     $this->assertInstanceOf('User', $user);
     // Assert they are not attached
     $this->assertFalse(in_array($client->_id, $user->client_ids));
     $this->assertFalse(in_array($user->_id, $client->user_ids));
     $this->assertCount(1, $user->clients);
     $this->assertCount(1, $client->users);
     // Attach the client to the user
     $user->clients()->attach($client);
     // Get the new user model
     $user = User::where('name', '=', 'Jane Doe')->first();
     $client = Client::Where('name', '=', 'Buffet Bar Inc.')->first();
     // Assert they are attached
     $this->assertTrue(in_array($client->_id, $user->client_ids));
     $this->assertTrue(in_array($user->_id, $client->user_ids));
     $this->assertCount(2, $user->clients);
     $this->assertCount(2, $client->users);
     // Detach clients from user
     $user->clients()->sync(array());
     // Get the new user model
     $user = User::where('name', '=', 'Jane Doe')->first();
     $client = Client::Where('name', '=', 'Buffet Bar Inc.')->first();
     // Assert they are not attached
     $this->assertFalse(in_array($client->_id, $user->client_ids));
     $this->assertFalse(in_array($user->_id, $client->user_ids));
     $this->assertCount(0, $user->clients);
     $this->assertCount(1, $client->users);
 }