diff --git a/src/Illuminate/Collections/Collection.php b/src/Illuminate/Collections/Collection.php index 0e18e2c2a1d8..23059c37eab1 100644 --- a/src/Illuminate/Collections/Collection.php +++ b/src/Illuminate/Collections/Collection.php @@ -1066,21 +1066,21 @@ public function splitIn($numberOfGroups) */ public function sole($key = null, $operator = null, $value = null) { - if (func_num_args() <= 1) { - $items = $this->when($key)->filter($key); + $filter = func_num_args() > 1 + ? $this->operatorForWhere(...func_get_args()) + : $key; - if ($items->isEmpty()) { - throw new ItemNotFoundException; - } + $items = $this->when($filter)->filter($filter); - if ($items->count() > 1) { - throw new MultipleItemsFoundException; - } + if ($items->isEmpty()) { + throw new ItemNotFoundException; + } - return $items->first(); + if ($items->count() > 1) { + throw new MultipleItemsFoundException; } - return $this->sole($this->operatorForWhere(...func_get_args())); + return $items->first(); } /** diff --git a/src/Illuminate/Collections/LazyCollection.php b/src/Illuminate/Collections/LazyCollection.php index 8046f3aa421b..76f970fef6c4 100644 --- a/src/Illuminate/Collections/LazyCollection.php +++ b/src/Illuminate/Collections/LazyCollection.php @@ -1024,16 +1024,16 @@ public function split($numberOfGroups) */ public function sole($key = null, $operator = null, $value = null) { - if (func_num_args() <= 1) { - return $this - ->when($key) - ->filter($key) - ->take(2) - ->collect() - ->sole(); - } - - return $this->sole($this->operatorForWhere(...func_get_args())); + $filter = func_num_args() > 1 + ? $this->operatorForWhere(...func_get_args()) + : $key; + + return $this + ->when($filter) + ->filter($filter) + ->take(2) + ->collect() + ->sole(); } /**