@@ -827,6 +827,21 @@ public function testWhereHasOnSelfReferencingBelongsToManyRelationship()
827
827
$ this ->
assertSame (
'[email protected] ' ,
$ results->
first ()->
email );
828
828
}
829
829
830
+ public function testWithWhereHasOnSelfReferencingBelongsToManyRelationship ()
831
+ {
832
+ $ user = EloquentTestUser::
create ([
'email ' =>
'[email protected] ' ]);
833
+ $ user->
friends ()->
create ([
'email ' =>
'[email protected] ' ]);
834
+
835
+ $ results = EloquentTestUser::withWhereHas ('friends ' , function ($ query ) {
836
+ $ query->
where (
'email ' ,
'[email protected] ' );
837
+ })->get ();
838
+
839
+ $ this ->assertCount (1 , $ results );
840
+ $ this ->
assertSame (
'[email protected] ' ,
$ results->
first ()->
email );
841
+ $ this ->assertTrue ($ results ->first ()->relationLoaded ('friends ' ));
842
+ $ this ->
assertSame (
$ results->
first ()->
friends ->
pluck (
'email ' )->
unique ()->
toArray (), [
'[email protected] ' ]);
843
+ }
844
+
830
845
public function testHasOnNestedSelfReferencingBelongsToManyRelationship ()
831
846
{
832
847
$ user = EloquentTestUser::
create ([
'email ' =>
'[email protected] ' ]);
@@ -853,6 +868,23 @@ public function testWhereHasOnNestedSelfReferencingBelongsToManyRelationship()
853
868
$ this ->
assertSame (
'[email protected] ' ,
$ results->
first ()->
email );
854
869
}
855
870
871
+ public function testWithWhereHasOnNestedSelfReferencingBelongsToManyRelationship ()
872
+ {
873
+ $ user = EloquentTestUser::
create ([
'email ' =>
'[email protected] ' ]);
874
+ $ friend =
$ user->
friends ()->
create ([
'email ' =>
'[email protected] ' ]);
875
+ $ friend->
friends ()->
create ([
'email ' =>
'[email protected] ' ]);
876
+
877
+ $ results = EloquentTestUser::withWhereHas ('friends.friends ' , function ($ query ) {
878
+ $ query->
where (
'email ' ,
'[email protected] ' );
879
+ })->get ();
880
+
881
+ $ this ->assertCount (1 , $ results );
882
+ $ this ->
assertSame (
'[email protected] ' ,
$ results->
first ()->
email );
883
+ $ this ->assertTrue ($ results ->first ()->relationLoaded ('friends ' ));
884
+ $ this ->
assertSame (
$ results->
first ()->
friends ->
pluck (
'email ' )->
unique ()->
toArray (), [
'[email protected] ' ]);
885
+ $ this ->
assertSame (
$ results->
first ()->
friends ->
pluck (
'friends ' )->
flatten ()->
pluck (
'email ' )->
unique ()->
toArray (), [
'[email protected] ' ]);
886
+ }
887
+
856
888
public function testHasOnSelfReferencingBelongsToManyRelationshipWithWherePivot ()
857
889
{
858
890
$ user = EloquentTestUser::
create ([
'email ' =>
'[email protected] ' ]);
@@ -909,6 +941,21 @@ public function testWhereHasOnSelfReferencingBelongsToRelationship()
909
941
$ this ->assertSame ('Child Post ' , $ results ->first ()->name );
910
942
}
911
943
944
+ public function testWithWhereHasOnSelfReferencingBelongsToRelationship ()
945
+ {
946
+ $ parentPost = EloquentTestPost::create (['name ' => 'Parent Post ' , 'user_id ' => 1 ]);
947
+ EloquentTestPost::create (['name ' => 'Child Post ' , 'parent_id ' => $ parentPost ->id , 'user_id ' => 2 ]);
948
+
949
+ $ results = EloquentTestPost::withWhereHas ('parentPost ' , function ($ query ) {
950
+ $ query ->where ('name ' , 'Parent Post ' );
951
+ })->get ();
952
+
953
+ $ this ->assertCount (1 , $ results );
954
+ $ this ->assertSame ('Child Post ' , $ results ->first ()->name );
955
+ $ this ->assertTrue ($ results ->first ()->relationLoaded ('parentPost ' ));
956
+ $ this ->assertSame ($ results ->first ()->parentPost ->name , 'Parent Post ' );
957
+ }
958
+
912
959
public function testHasOnNestedSelfReferencingBelongsToRelationship ()
913
960
{
914
961
$ grandParentPost = EloquentTestPost::create (['name ' => 'Grandparent Post ' , 'user_id ' => 1 ]);
@@ -935,6 +982,24 @@ public function testWhereHasOnNestedSelfReferencingBelongsToRelationship()
935
982
$ this ->assertSame ('Child Post ' , $ results ->first ()->name );
936
983
}
937
984
985
+ public function testWithWhereHasOnNestedSelfReferencingBelongsToRelationship ()
986
+ {
987
+ $ grandParentPost = EloquentTestPost::create (['name ' => 'Grandparent Post ' , 'user_id ' => 1 ]);
988
+ $ parentPost = EloquentTestPost::create (['name ' => 'Parent Post ' , 'parent_id ' => $ grandParentPost ->id , 'user_id ' => 2 ]);
989
+ EloquentTestPost::create (['name ' => 'Child Post ' , 'parent_id ' => $ parentPost ->id , 'user_id ' => 3 ]);
990
+
991
+ $ results = EloquentTestPost::withWhereHas ('parentPost.parentPost ' , function ($ query ) {
992
+ $ query ->where ('name ' , 'Grandparent Post ' );
993
+ })->get ();
994
+
995
+ $ this ->assertCount (1 , $ results );
996
+ $ this ->assertSame ('Child Post ' , $ results ->first ()->name );
997
+ $ this ->assertTrue ($ results ->first ()->relationLoaded ('parentPost ' ));
998
+ $ this ->assertSame ($ results ->first ()->parentPost ->name , 'Parent Post ' );
999
+ $ this ->assertTrue ($ results ->first ()->parentPost ->relationLoaded ('parentPost ' ));
1000
+ $ this ->assertSame ($ results ->first ()->parentPost ->parentPost ->name , 'Grandparent Post ' );
1001
+ }
1002
+
938
1003
public function testHasOnSelfReferencingHasManyRelationship ()
939
1004
{
940
1005
$ parentPost = EloquentTestPost::create (['name ' => 'Parent Post ' , 'user_id ' => 1 ]);
@@ -959,6 +1024,21 @@ public function testWhereHasOnSelfReferencingHasManyRelationship()
959
1024
$ this ->assertSame ('Parent Post ' , $ results ->first ()->name );
960
1025
}
961
1026
1027
+ public function testWithWhereHasOnSelfReferencingHasManyRelationship ()
1028
+ {
1029
+ $ parentPost = EloquentTestPost::create (['name ' => 'Parent Post ' , 'user_id ' => 1 ]);
1030
+ EloquentTestPost::create (['name ' => 'Child Post ' , 'parent_id ' => $ parentPost ->id , 'user_id ' => 2 ]);
1031
+
1032
+ $ results = EloquentTestPost::withWhereHas ('childPosts ' , function ($ query ) {
1033
+ $ query ->where ('name ' , 'Child Post ' );
1034
+ })->get ();
1035
+
1036
+ $ this ->assertCount (1 , $ results );
1037
+ $ this ->assertSame ('Parent Post ' , $ results ->first ()->name );
1038
+ $ this ->assertTrue ($ results ->first ()->relationLoaded ('childPosts ' ));
1039
+ $ this ->assertSame ($ results ->first ()->childPosts ->pluck ('name ' )->unique ()->toArray (), ['Child Post ' ]);
1040
+ }
1041
+
962
1042
public function testHasOnNestedSelfReferencingHasManyRelationship ()
963
1043
{
964
1044
$ grandParentPost = EloquentTestPost::create (['name ' => 'Grandparent Post ' , 'user_id ' => 1 ]);
@@ -985,6 +1065,23 @@ public function testWhereHasOnNestedSelfReferencingHasManyRelationship()
985
1065
$ this ->assertSame ('Grandparent Post ' , $ results ->first ()->name );
986
1066
}
987
1067
1068
+ public function testWithWhereHasOnNestedSelfReferencingHasManyRelationship ()
1069
+ {
1070
+ $ grandParentPost = EloquentTestPost::create (['name ' => 'Grandparent Post ' , 'user_id ' => 1 ]);
1071
+ $ parentPost = EloquentTestPost::create (['name ' => 'Parent Post ' , 'parent_id ' => $ grandParentPost ->id , 'user_id ' => 2 ]);
1072
+ EloquentTestPost::create (['name ' => 'Child Post ' , 'parent_id ' => $ parentPost ->id , 'user_id ' => 3 ]);
1073
+
1074
+ $ results = EloquentTestPost::withWhereHas ('childPosts.childPosts ' , function ($ query ) {
1075
+ $ query ->where ('name ' , 'Child Post ' );
1076
+ })->get ();
1077
+
1078
+ $ this ->assertCount (1 , $ results );
1079
+ $ this ->assertSame ('Grandparent Post ' , $ results ->first ()->name );
1080
+ $ this ->assertTrue ($ results ->first ()->relationLoaded ('childPosts ' ));
1081
+ $ this ->assertSame ($ results ->first ()->childPosts ->pluck ('name ' )->unique ()->toArray (), ['Parent Post ' ]);
1082
+ $ this ->assertSame ($ results ->first ()->childPosts ->pluck ('childPosts ' )->flatten ()->pluck ('name ' )->unique ()->toArray (), ['Child Post ' ]);
1083
+ }
1084
+
988
1085
public function testHasWithNonWhereBindings ()
989
1086
{
990
1087
$ user = EloquentTestUser::
create ([
'id ' =>
1 ,
'email ' =>
'[email protected] ' ]);
0 commit comments