@@ -86,6 +86,44 @@ public function testOrWhereRelationCallback($callbackEloquent, $callbackQuery)
86
86
$ this ->assertEquals ($ userOrWhereHas ->first ()->id , $ query ->first ()->id );
87
87
}
88
88
89
+ /**
90
+ * Check that the 'whereDoesntHaveRelation' callback function works.
91
+ */
92
+ #[DataProvider('dataProviderWhereRelationCallback ' )]
93
+ public function testWhereDoesntRelationCallback ($ callbackEloquent , $ callbackQuery )
94
+ {
95
+ $ userWhereDoesntRelation = User::whereDoesntHaveRelation ('posts ' , $ callbackEloquent );
96
+ $ userWhereHas = User::whereDoesntHave ('posts ' , $ callbackEloquent );
97
+ $ query = DB ::table ('users ' )->whereNotExists ($ callbackQuery );
98
+
99
+ $ this ->assertEquals ($ userWhereDoesntRelation ->getQuery ()->toSql (), $ query ->toSql ());
100
+ $ this ->assertEquals ($ userWhereDoesntRelation ->getQuery ()->toSql (), $ userWhereHas ->toSql ());
101
+ $ this ->assertEquals ($ userWhereHas ->getQuery ()->toSql (), $ query ->toSql ());
102
+
103
+ $ this ->assertEquals ($ userWhereDoesntRelation ->first ()->id , $ query ->first ()->id );
104
+ $ this ->assertEquals ($ userWhereDoesntRelation ->first ()->id , $ userWhereHas ->first ()->id );
105
+ $ this ->assertEquals ($ userWhereHas ->first ()->id , $ query ->first ()->id );
106
+ }
107
+
108
+ /**
109
+ * Check that the 'orWhereDoesntRelation' callback function works.
110
+ */
111
+ #[DataProvider('dataProviderWhereRelationCallback ' )]
112
+ public function testOrWhereDoesntRelationCallback ($ callbackEloquent , $ callbackQuery )
113
+ {
114
+ $ userOrWhereDoesntRelation = User::orWhereDoesntHaveRelation ('posts ' , $ callbackEloquent );
115
+ $ userOrWhereHas = User::orWhereDoesntHave ('posts ' , $ callbackEloquent );
116
+ $ query = DB ::table ('users ' )->orWhereNotExists ($ callbackQuery );
117
+
118
+ $ this ->assertEquals ($ userOrWhereDoesntRelation ->getQuery ()->toSql (), $ query ->toSql ());
119
+ $ this ->assertEquals ($ userOrWhereDoesntRelation ->getQuery ()->toSql (), $ userOrWhereHas ->toSql ());
120
+ $ this ->assertEquals ($ userOrWhereHas ->getQuery ()->toSql (), $ query ->toSql ());
121
+
122
+ $ this ->assertEquals ($ userOrWhereDoesntRelation ->first ()->id , $ query ->first ()->id );
123
+ $ this ->assertEquals ($ userOrWhereDoesntRelation ->first ()->id , $ userOrWhereHas ->first ()->id );
124
+ $ this ->assertEquals ($ userOrWhereHas ->first ()->id , $ query ->first ()->id );
125
+ }
126
+
89
127
public static function dataProviderWhereRelationCallback ()
90
128
{
91
129
$ callbackArray = function ($ value ) {
@@ -158,6 +196,50 @@ public function testOrWhereMorphRelation()
158
196
$ this ->assertEquals ([1 , 2 ], $ comments ->pluck ('id ' )->all ());
159
197
}
160
198
199
+ public function testWhereDoesntHaveRelation ()
200
+ {
201
+ $ users = User::whereDoesntHaveRelation ('posts ' , 'public ' , true )->get ();
202
+
203
+ $ this ->assertEquals ([2 ], $ users ->pluck ('id ' )->all ());
204
+ }
205
+
206
+ public function testOrWhereDoesntHaveRelation ()
207
+ {
208
+ $ users = User::whereDoesntHaveRelation ('posts ' , 'public ' , true )->orWhereDoesntHaveRelation ('posts ' , 'public ' , false )->get ();
209
+
210
+ $ this ->assertEquals ([1 , 2 ], $ users ->pluck ('id ' )->all ());
211
+ }
212
+
213
+ public function testNestedWhereDoesntHaveRelation ()
214
+ {
215
+ $ texts = User::whereDoesntHaveRelation ('posts.texts ' , 'content ' , 'test ' )->get ();
216
+
217
+ $ this ->assertEquals ([2 ], $ texts ->pluck ('id ' )->all ());
218
+ }
219
+
220
+ public function testNestedOrWhereDoesntHaveRelation ()
221
+ {
222
+ $ texts = User::whereDoesntHaveRelation ('posts.texts ' , 'content ' , 'test ' )->orWhereDoesntHaveRelation ('posts.texts ' , 'content ' , 'test2 ' )->get ();
223
+
224
+ $ this ->assertEquals ([1 , 2 ], $ texts ->pluck ('id ' )->all ());
225
+ }
226
+
227
+ public function testWhereMorphDoesntHaveRelation ()
228
+ {
229
+ $ comments = Comment::whereMorphDoesntHaveRelation ('commentable ' , '* ' , 'public ' , true )->get ();
230
+
231
+ $ this ->assertEquals ([2 ], $ comments ->pluck ('id ' )->all ());
232
+ }
233
+
234
+ public function testOrWhereMorphDoesntHaveRelation ()
235
+ {
236
+ $ comments = Comment::whereMorphDoesntHaveRelation ('commentable ' , '* ' , 'public ' , true )
237
+ ->orWhereMorphDoesntHaveRelation ('commentable ' , '* ' , 'public ' , false )
238
+ ->get ();
239
+
240
+ $ this ->assertEquals ([1 , 2 ], $ comments ->pluck ('id ' )->all ());
241
+ }
242
+
161
243
public function testWithCount ()
162
244
{
163
245
$ users = User::whereHas ('posts ' , function ($ query ) {
0 commit comments