@@ -27,13 +27,8 @@ trait QueriesRelationships
2727 *
2828 * @throws Exception
2929 */
30- public function has (
31- $ relation ,
32- $ operator = '>= ' ,
33- $ count = 1 ,
34- $ boolean = 'and ' ,
35- ?Closure $ callback = null
36- ): Builder |static {
30+ public function has ($ relation , $ operator = '>= ' , $ count = 1 , $ boolean = 'and ' , ?Closure $ callback = null ): Builder |static
31+ {
3732 if (is_string ($ relation )) {
3833 if (str_contains ($ relation , '. ' )) {
3934 // @phpstan-ignore-next-line
@@ -79,13 +74,8 @@ protected function isAcrossConnections(Relation $relation): bool
7974 *
8075 * @throws Exception
8176 */
82- public function addHybridHas (
83- Relation $ relation ,
84- string $ operator = '>= ' ,
85- int $ count = 1 ,
86- string $ boolean = 'and ' ,
87- ?Closure $ callback = null
88- ): mixed {
77+ public function addHybridHas (Relation $ relation , string $ operator = '>= ' , int $ count = 1 , string $ boolean = 'and ' , ?Closure $ callback = null ): mixed
78+ {
8979 $ hasQuery = $ relation ->getQuery ();
9080 if ($ callback ) {
9181 $ hasQuery ->callScope ($ callback );
@@ -102,14 +92,22 @@ public function addHybridHas(
10292 $ relation instanceof MorphToMany => $ relation ->getInverse () ?
10393 $ this ->handleMorphedByMany ($ hasQuery , $ relation ) :
10494 $ this ->handleMorphToMany ($ hasQuery , $ relation ),
105- default => $ hasQuery -> pluck ( $ this ->getHasCompareKey ( $ relation) )
95+ default => $ this ->handleDefaultForeignIdsLookup ( $ hasQuery , $ relation )
10696 };
10797
10898 $ relatedIds = $ this ->getConstrainedRelatedIds ($ relations , $ operator , $ count );
10999
110100 return $ this ->whereIn ($ this ->getRelatedConstraintKey ($ relation ), $ relatedIds , $ boolean , $ not );
111101 }
112102
103+ private function handleDefaultForeignIdsLookup ($ query , $ relation )
104+ {
105+ $ key = $ this ->getHasCompareKey ($ relation );
106+ $ query ->whereNotNull ($ key );
107+
108+ return $ query ->pluck ($ key );
109+ }
110+
113111 /**
114112 * @param Builder $hasQuery
115113 * @param Relation $relation
0 commit comments