From 2a922bc0d83ebdf4199ff8fa75088b27c9c41083 Mon Sep 17 00:00:00 2001 From: Alex Bowers Date: Thu, 30 Apr 2020 22:58:40 +0100 Subject: [PATCH 1/7] Add hasScope function to the Base Model --- src/Illuminate/Database/Eloquent/Builder.php | 4 +-- src/Illuminate/Database/Eloquent/Model.php | 11 +++++++ .../Database/EloquentModelScopeTest.php | 33 +++++++++++++++++++ 3 files changed, 46 insertions(+), 2 deletions(-) create mode 100644 tests/Integration/Database/EloquentModelScopeTest.php diff --git a/src/Illuminate/Database/Eloquent/Builder.php b/src/Illuminate/Database/Eloquent/Builder.php index bbe20a83c6bd..8ec02d6e5bed 100755 --- a/src/Illuminate/Database/Eloquent/Builder.php +++ b/src/Illuminate/Database/Eloquent/Builder.php @@ -1373,8 +1373,8 @@ public function __call($method, $parameters) return call_user_func_array(static::$macros[$method], $parameters); } - if (method_exists($this->model, $scope = 'scope'.ucfirst($method))) { - return $this->callScope([$this->model, $scope], $parameters); + if ($this->model->hasScope($method)) { + return $this->callScope([$this->model, 'scope' . ucfirst($method)], $parameters); } if (in_array($method, $this->passthru)) { diff --git a/src/Illuminate/Database/Eloquent/Model.php b/src/Illuminate/Database/Eloquent/Model.php index 98fc24974278..a32705f1bc66 100644 --- a/src/Illuminate/Database/Eloquent/Model.php +++ b/src/Illuminate/Database/Eloquent/Model.php @@ -331,6 +331,17 @@ public static function isIgnoringTouch($class = null) return false; } + /** + * Determine if the given model has a scope. + * + * @param string $method + * @return bool + */ + public function hasScope(string $method) + { + return method_exists($this, 'scope' . ucfirst($method)); + } + /** * Fill the model with an array of attributes. * diff --git a/tests/Integration/Database/EloquentModelScopeTest.php b/tests/Integration/Database/EloquentModelScopeTest.php new file mode 100644 index 000000000000..c5b3aee44112 --- /dev/null +++ b/tests/Integration/Database/EloquentModelScopeTest.php @@ -0,0 +1,33 @@ +assertTrue($model->hasScope("exists")); + } + + public function testModelDoesNotHaveScope() + { + $model = new TestModel1; + + $this->assertFalse($model->hasScope("doesNotExist")); + } +} + +class TestModel1 extends Model +{ + public function scopeExists() + { + return true; + } +} From d370eb18092210adadcbdd857f97d4c1dc2784c3 Mon Sep 17 00:00:00 2001 From: Alex Bowers Date: Fri, 1 May 2020 01:54:19 +0100 Subject: [PATCH 2/7] Add hasScope to the builder --- src/Illuminate/Database/Eloquent/Builder.php | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/Illuminate/Database/Eloquent/Builder.php b/src/Illuminate/Database/Eloquent/Builder.php index 8ec02d6e5bed..297777e3798c 100755 --- a/src/Illuminate/Database/Eloquent/Builder.php +++ b/src/Illuminate/Database/Eloquent/Builder.php @@ -1327,6 +1327,17 @@ public static function hasGlobalMacro($name) return isset(static::$macros[$name]); } + /** + * Determine if the given model has a scope. + * + * @param string $method + * @return bool + */ + public function hasScope(string $name) + { + return $this->model->hasScope($name); + } + /** * Dynamically access builder proxies. * @@ -1373,7 +1384,7 @@ public function __call($method, $parameters) return call_user_func_array(static::$macros[$method], $parameters); } - if ($this->model->hasScope($method)) { + if ($this->hasScope($method)) { return $this->callScope([$this->model, 'scope' . ucfirst($method)], $parameters); } From 75895929ef331ddff53059dd6866f2164e3f82de Mon Sep 17 00:00:00 2001 From: Alex Bowers Date: Fri, 1 May 2020 02:06:41 +0100 Subject: [PATCH 3/7] Only check for scope if model exists --- src/Illuminate/Database/Eloquent/Builder.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Illuminate/Database/Eloquent/Builder.php b/src/Illuminate/Database/Eloquent/Builder.php index 297777e3798c..20fce5465ae4 100755 --- a/src/Illuminate/Database/Eloquent/Builder.php +++ b/src/Illuminate/Database/Eloquent/Builder.php @@ -1335,7 +1335,7 @@ public static function hasGlobalMacro($name) */ public function hasScope(string $name) { - return $this->model->hasScope($name); + return $this->model && $this->model->hasScope($name); } /** From 358827f3b0e44eeb166e6ef70d720213b5b5d3a7 Mon Sep 17 00:00:00 2001 From: Alex Bowers Date: Fri, 1 May 2020 02:06:48 +0100 Subject: [PATCH 4/7] Fix duplicate model name --- tests/Integration/Database/EloquentModelScopeTest.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/Integration/Database/EloquentModelScopeTest.php b/tests/Integration/Database/EloquentModelScopeTest.php index c5b3aee44112..deea8307ac82 100644 --- a/tests/Integration/Database/EloquentModelScopeTest.php +++ b/tests/Integration/Database/EloquentModelScopeTest.php @@ -11,20 +11,20 @@ class EloquentModelScopeTest extends DatabaseTestCase { public function testModelHasScope() { - $model = new TestModel1; + $model = new TestScopeModel1; $this->assertTrue($model->hasScope("exists")); } public function testModelDoesNotHaveScope() { - $model = new TestModel1; + $model = new TestScopeModel1; $this->assertFalse($model->hasScope("doesNotExist")); } } -class TestModel1 extends Model +class TestScopeModel1 extends Model { public function scopeExists() { From d2eb5593808498ac236db9fcb208cd2917490e83 Mon Sep 17 00:00:00 2001 From: Alex Bowers Date: Fri, 1 May 2020 09:10:46 +0100 Subject: [PATCH 5/7] Formatting --- src/Illuminate/Database/Eloquent/Builder.php | 4 ++-- src/Illuminate/Database/Eloquent/Model.php | 4 ++-- tests/Integration/Database/EloquentModelScopeTest.php | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Illuminate/Database/Eloquent/Builder.php b/src/Illuminate/Database/Eloquent/Builder.php index 20fce5465ae4..590613e019a9 100755 --- a/src/Illuminate/Database/Eloquent/Builder.php +++ b/src/Illuminate/Database/Eloquent/Builder.php @@ -1329,7 +1329,7 @@ public static function hasGlobalMacro($name) /** * Determine if the given model has a scope. - * + * * @param string $method * @return bool */ @@ -1385,7 +1385,7 @@ public function __call($method, $parameters) } if ($this->hasScope($method)) { - return $this->callScope([$this->model, 'scope' . ucfirst($method)], $parameters); + return $this->callScope([$this->model, 'scope'.ucfirst($method)], $parameters); } if (in_array($method, $this->passthru)) { diff --git a/src/Illuminate/Database/Eloquent/Model.php b/src/Illuminate/Database/Eloquent/Model.php index a32705f1bc66..682aa068b6f4 100644 --- a/src/Illuminate/Database/Eloquent/Model.php +++ b/src/Illuminate/Database/Eloquent/Model.php @@ -333,13 +333,13 @@ public static function isIgnoringTouch($class = null) /** * Determine if the given model has a scope. - * + * * @param string $method * @return bool */ public function hasScope(string $method) { - return method_exists($this, 'scope' . ucfirst($method)); + return method_exists($this, 'scope'.ucfirst($method)); } /** diff --git a/tests/Integration/Database/EloquentModelScopeTest.php b/tests/Integration/Database/EloquentModelScopeTest.php index deea8307ac82..a51f074464e8 100644 --- a/tests/Integration/Database/EloquentModelScopeTest.php +++ b/tests/Integration/Database/EloquentModelScopeTest.php @@ -13,14 +13,14 @@ public function testModelHasScope() { $model = new TestScopeModel1; - $this->assertTrue($model->hasScope("exists")); + $this->assertTrue($model->hasScope('exists')); } public function testModelDoesNotHaveScope() { $model = new TestScopeModel1; - $this->assertFalse($model->hasScope("doesNotExist")); + $this->assertFalse($model->hasScope('doesNotExist')); } } From 469d45ca59c9bff87c6df344f4d91011ef45a080 Mon Sep 17 00:00:00 2001 From: Graham Campbell Date: Fri, 1 May 2020 09:54:01 +0100 Subject: [PATCH 6/7] Update Builder.php --- src/Illuminate/Database/Eloquent/Builder.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Illuminate/Database/Eloquent/Builder.php b/src/Illuminate/Database/Eloquent/Builder.php index 590613e019a9..4b7af9b37050 100755 --- a/src/Illuminate/Database/Eloquent/Builder.php +++ b/src/Illuminate/Database/Eloquent/Builder.php @@ -1330,10 +1330,10 @@ public static function hasGlobalMacro($name) /** * Determine if the given model has a scope. * - * @param string $method + * @param string $name * @return bool */ - public function hasScope(string $name) + public function hasScope($name) { return $this->model && $this->model->hasScope($name); } From bbdc776a3e0d56862055b9c7d71a7af51554176e Mon Sep 17 00:00:00 2001 From: Graham Campbell Date: Fri, 1 May 2020 09:54:38 +0100 Subject: [PATCH 7/7] Update Model.php --- src/Illuminate/Database/Eloquent/Model.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Illuminate/Database/Eloquent/Model.php b/src/Illuminate/Database/Eloquent/Model.php index 682aa068b6f4..c2870c108015 100644 --- a/src/Illuminate/Database/Eloquent/Model.php +++ b/src/Illuminate/Database/Eloquent/Model.php @@ -334,10 +334,10 @@ public static function isIgnoringTouch($class = null) /** * Determine if the given model has a scope. * - * @param string $method + * @param string $method * @return bool */ - public function hasScope(string $method) + public function hasScope($method) { return method_exists($this, 'scope'.ucfirst($method)); }