@@ -134,7 +134,6 @@ public function testHasManyAndBelongsTo()
134
134
$ user ->clients ()->create (array ('name ' => 'Buffet Bar Inc. ' ));
135
135
136
136
$ user = User::with ('clients ' )->find ($ user ->_id );
137
-
138
137
$ client = Client::with ('users ' )->first ();
139
138
140
139
$ clients = $ client ->getRelation ('users ' );
@@ -148,15 +147,13 @@ public function testHasManyAndBelongsTo()
148
147
$ this ->assertCount (1 , $ client ->users );
149
148
150
149
// Now create a new user to an existing client
151
- $ client ->users ()->create (array ('name ' => 'Jane Doe ' ));
152
-
153
- $ otherClient = User::where ('name ' , '= ' , 'Jane Doe ' )->first ()->clients ()->get ();
150
+ $ user = $ client ->users ()->create (array ('name ' => 'Jane Doe ' ));
154
151
155
- $ this ->assertInstanceOf ('Illuminate\Database\Eloquent\Collection ' , $ otherClient );
156
- $ this ->assertInstanceOf ('Client ' , $ otherClient [ 0 ] );
157
- $ this ->assertCount (1 , $ otherClient );
152
+ $ this ->assertInstanceOf ('Illuminate\Database\Eloquent\Collection ' , $ user -> clients );
153
+ $ this ->assertInstanceOf ('Client ' , $ user -> clients -> first () );
154
+ $ this ->assertCount (1 , $ user -> clients );
158
155
159
- // Now attach an existing client to an existing user
156
+ // Get user and unattached client
160
157
$ user = User::where ('name ' , '= ' , 'Jane Doe ' )->first ();
161
158
$ client = Client::Where ('name ' , '= ' , 'Buffet Bar Inc. ' )->first ();
162
159
@@ -167,6 +164,8 @@ public function testHasManyAndBelongsTo()
167
164
// Assert they are not attached
168
165
$ this ->assertFalse (in_array ($ client ->_id , $ user ->client_ids ));
169
166
$ this ->assertFalse (in_array ($ user ->_id , $ client ->user_ids ));
167
+ $ this ->assertCount (1 , $ user ->clients );
168
+ $ this ->assertCount (1 , $ client ->users );
170
169
171
170
// Attach the client to the user
172
171
$ user ->clients ()->attach ($ client );
@@ -178,6 +177,21 @@ public function testHasManyAndBelongsTo()
178
177
// Assert they are attached
179
178
$ this ->assertTrue (in_array ($ client ->_id , $ user ->client_ids ));
180
179
$ this ->assertTrue (in_array ($ user ->_id , $ client ->user_ids ));
180
+ $ this ->assertCount (2 , $ user ->clients );
181
+ $ this ->assertCount (2 , $ client ->users );
182
+
183
+ // Detach clients from user
184
+ $ user ->clients ()->sync (array ());
185
+
186
+ // Get the new user model
187
+ $ user = User::where ('name ' , '= ' , 'Jane Doe ' )->first ();
188
+ $ client = Client::Where ('name ' , '= ' , 'Buffet Bar Inc. ' )->first ();
189
+
190
+ // Assert they are not attached
191
+ $ this ->assertFalse (in_array ($ client ->_id , $ user ->client_ids ));
192
+ $ this ->assertFalse (in_array ($ user ->_id , $ client ->user_ids ));
193
+ $ this ->assertCount (0 , $ user ->clients );
194
+ $ this ->assertCount (1 , $ client ->users );
181
195
}
182
196
183
197
public function testHasManyAndBelongsToAttachesExistingModels ()
@@ -205,6 +219,7 @@ public function testHasManyAndBelongsToAttachesExistingModels()
205
219
// Assert there are two client objects in the relationship
206
220
$ this ->assertCount (2 , $ user ->clients );
207
221
222
+ // Add more clients
208
223
$ user ->clients ()->sync ($ moreClients );
209
224
210
225
$ user = User::with ('clients ' )->find ($ user ->_id );
0 commit comments