@@ -280,6 +280,35 @@ public function testBelongsToManySync()
280
280
$ this ->assertCount (1 , $ user ->clients );
281
281
}
282
282
283
+ public function testBelongsToManySyncAttrs ()
284
+ {
285
+ // create test instances
286
+ /** @var User $user */
287
+ $ user = User::create (['name ' => 'John Doe ' ]);
288
+ $ client1 = Client::create (['name ' => 'Pork Pies Ltd. ' ])->_id ;
289
+ $ client2 = Client::create (['name ' => 'Buffet Bar Inc. ' ])->_id ;
290
+
291
+ // Sync multiple
292
+ $ user ->clients ()->sync ([$ client1 => ['fresh_client ' => true ], $ client2 => ['fresh_client ' => false ]]);
293
+
294
+ //Check Sync Success
295
+ $ this ->assertEquals ($ user ->client_ids [0 ]['_id ' ], $ client1 );
296
+ $ this ->assertEquals ($ user ->client_ids [1 ]['_id ' ], $ client2 );
297
+
298
+ //Check reverse
299
+ $ this ->assertEquals ($ user ->_id , Client::find ($ client1 )->user_ids [0 ]['_id ' ]);
300
+ $ this ->assertEquals ($ user ->_id , Client::find ($ client2 )->user_ids [0 ]['_id ' ]);
301
+
302
+ $ clients = $ user ->clients ;
303
+ $ this ->assertCount (2 , $ clients );
304
+
305
+ foreach ($ user ->clients ()->withPivot ('fresh_client ' )->get () as $ item ) {
306
+ $ this ->assertIsArray ($ item ->pivot ->toArray ());
307
+ $ this ->assertEquals ($ item ->_id == $ client1 , $ item ->pivot ->fresh_client );
308
+ }
309
+ $ this ->assertTrue (true );
310
+ }
311
+
283
312
public function testBelongsToManyAttachArray ()
284
313
{
285
314
$ user = User::create (['name ' => 'John Doe ' ]);
@@ -369,7 +398,7 @@ public function testMorph()
369
398
$ this ->assertEquals ($ photo ->imageable ->name , $ user ->name );
370
399
371
400
$ user = User::with ('photos ' )->find ($ user ->_id );
372
- $ relations = $ user ->getRelations ();
401
+ $ relations = $ user ->getRlations ();
373
402
$ this ->assertArrayHasKey ('photos ' , $ relations );
374
403
$ this ->assertEquals (1 , $ relations ['photos ' ]->count ());
375
404
0 commit comments