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); }
public function testNestedKeys() { $client = Client::create(array('data' => array('client_id' => 35298, 'name' => 'John Doe'))); $address = $client->addresses()->create(array('data' => array('address_id' => 1432, 'city' => 'Paris'))); $client = Client::where('data.client_id', 35298)->first(); $this->assertEquals(1, $client->addresses->count()); $address = $client->addresses->first(); $this->assertEquals('Paris', $address->data['city']); $client = Client::with('addresses')->first(); $this->assertEquals('Paris', $client->addresses->first()->data['city']); }
public function getWaitlist() { $clients = Client::with('user')->where('on_waitlist', '=', '1')->orderBy('id')->paginate(10); $this->layout->content = View::make('admin.dashboard')->with('title', 'Clients')->with('hasUsersInWaitlist', $this->hasUsersInWaitlist)->nest('innerContent', 'admin.waitlist', array('clients' => $clients)); }