@@ -803,7 +803,7 @@ public function where($column, $operator = null, $value = null, $boolean = 'and'
803
803
// If the value is a Closure, it means the developer is performing an entire
804
804
// sub-select within the query and we will need to compile the sub-select
805
805
// within the where clause to get the appropriate query record results.
806
- if ($ value instanceof Closure ) {
806
+ if ($ this -> isQueryable ( $ value) ) {
807
807
return $ this ->whereSub ($ column , $ operator , $ value , $ boolean );
808
808
}
809
809
@@ -1658,18 +1658,22 @@ public function addNestedWhereQuery($query, $boolean = 'and')
1658
1658
*
1659
1659
* @param \Illuminate\Contracts\Database\Query\Expression|string $column
1660
1660
* @param string $operator
1661
- * @param \Closure $callback
1661
+ * @param \Closure||\Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder $callback
1662
1662
* @param string $boolean
1663
1663
* @return $this
1664
1664
*/
1665
- protected function whereSub ($ column , $ operator , Closure $ callback , $ boolean )
1665
+ protected function whereSub ($ column , $ operator , $ callback , $ boolean )
1666
1666
{
1667
1667
$ type = 'Sub ' ;
1668
1668
1669
- // Once we have the query instance we can simply execute it so it can add all
1670
- // of the sub-select's conditions to itself, and then we can cache it off
1671
- // in the array of where clauses for the "main" parent query instance.
1672
- $ callback ($ query = $ this ->forSubQuery ());
1669
+ if ($ callback instanceof Closure) {
1670
+ // Once we have the query instance we can simply execute it so it can add all
1671
+ // of the sub-select's conditions to itself, and then we can cache it off
1672
+ // in the array of where clauses for the "main" parent query instance.
1673
+ $ callback ($ query = $ this ->forSubQuery ());
1674
+ } else {
1675
+ $ query = $ callback instanceof EloquentBuilder ? $ callback ->toBase () : $ callback ;
1676
+ }
1673
1677
1674
1678
$ this ->wheres [] = compact (
1675
1679
'type ' , 'column ' , 'operator ' , 'query ' , 'boolean '
0 commit comments