@@ -597,6 +597,11 @@ public function orWhere($column, $operator = null, $value = null)
597597 */
598598 public function has ($ relation , $ operator = '>= ' , $ count = 1 , $ boolean = 'and ' , Closure $ callback = null )
599599 {
600+ if (strpos ($ relation , '. ' ) !== false )
601+ {
602+ return $ this ->hasNested ($ relation , $ operator , $ count , $ boolean , $ callback );
603+ }
604+
600605 $ relation = $ this ->getHasRelationQuery ($ relation );
601606
602607 $ query = $ relation ->getRelationCountQuery ($ relation ->getRelated ()->newQuery (), $ this );
@@ -606,6 +611,41 @@ public function has($relation, $operator = '>=', $count = 1, $boolean = 'and', C
606611 return $ this ->addHasWhere ($ query , $ relation , $ operator , $ count , $ boolean );
607612 }
608613
614+ /**
615+ * Add nested relationship count conditions to the query.
616+ *
617+ * @param string $relations
618+ * @param string $operator
619+ * @param integer $count
620+ * @param string $boolean
621+ * @param \Closure $callback
622+ * @return \Illuminate\Database\Eloquent\Builder|static
623+ */
624+ protected function hasNested ($ relations , $ operator = '>= ' , $ count = 1 , $ boolean = 'and ' , $ callback = null )
625+ {
626+ $ relations = explode ('. ' , $ relations );
627+
628+ // In order to nest "has", we need to add count relation constraints
629+ // on the callback closure. We will do this by simply passing to
630+ // closure its own reference, so it calls itself recursively.
631+ $ closure = function ($ q ) use (&$ closure , &$ relations , $ operator , $ count , $ boolean , $ callback )
632+ {
633+ // If the "relation" is specified using dot notation, we will assume
634+ // that developer wants to check simple "has" on the intermediate
635+ // relations and add constraints only on the furthermost query.
636+ if (count ($ relations ) > 1 )
637+ {
638+ $ q ->whereHas (array_shift ($ relations ), $ closure );
639+ }
640+ else
641+ {
642+ $ q ->has (array_shift ($ relations ), $ operator , $ count , $ boolean , $ callback );
643+ }
644+ };
645+
646+ return $ this ->whereHas (array_shift ($ relations ), $ closure );
647+ }
648+
609649 /**
610650 * Add a relationship count condition to the query.
611651 *
0 commit comments