@@ -322,7 +322,7 @@ export class ModelSchema<M extends BaseModel = any> extends Macroable {
322322 * that are included.
323323 */
324324 public getIncludedRelations ( ) : RelationOptions [ ] {
325- return this . relations . filter ( r => r . isIncluded )
325+ return this . relations . filter ( r => r . isIncluded || r . isWhereHasIncluded )
326326 }
327327
328328 /**
@@ -369,6 +369,43 @@ export class ModelSchema<M extends BaseModel = any> extends Macroable {
369369 return options
370370 }
371371
372+ /**
373+ * Include a relation by setting the isWhereHasIncluded
374+ * option to true.
375+ */
376+ public includeWhereHasRelation (
377+ property : string | ModelRelations < M > ,
378+ closure ?: ( query : ModelQueryBuilder ) => any
379+ ) {
380+ const model = this . Model . name
381+
382+ if ( property . includes ( '.' ) ) {
383+ const [ first , ...rest ] = property . split ( '.' )
384+
385+ property = first
386+ closure = this . createdNestedRelationClosure ( rest )
387+ }
388+
389+ const options = this . getRelationByProperty ( property )
390+
391+ if ( ! options ) {
392+ throw new NotImplementedRelationException (
393+ property as string ,
394+ model ,
395+ this . relations . map ( r => r . property ) . join ( ', ' )
396+ )
397+ }
398+
399+ const i = this . relations . indexOf ( options )
400+
401+ options . isWhereHasIncluded = true
402+ options . closure = closure
403+
404+ this . relations [ i ] = options
405+
406+ return options
407+ }
408+
372409 /**
373410 * Created nested relationships closure to
374411 * load relationship's relationships
0 commit comments