diff --git a/src/Illuminate/Database/Eloquent/Builder.php b/src/Illuminate/Database/Eloquent/Builder.php index bbe20a83c6bd..4b7af9b37050 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 $name + * @return bool + */ + public function hasScope($name) + { + return $this->model && $this->model->hasScope($name); + } + /** * Dynamically access builder proxies. * @@ -1373,8 +1384,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->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..c2870c108015 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($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..a51f074464e8 --- /dev/null +++ b/tests/Integration/Database/EloquentModelScopeTest.php @@ -0,0 +1,33 @@ +assertTrue($model->hasScope('exists')); + } + + public function testModelDoesNotHaveScope() + { + $model = new TestScopeModel1; + + $this->assertFalse($model->hasScope('doesNotExist')); + } +} + +class TestScopeModel1 extends Model +{ + public function scopeExists() + { + return true; + } +}