Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[7.x] Add hasScope function to the Base Model #32622

Merged
merged 7 commits into from
May 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions src/Illuminate/Database/Eloquent/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Kinda odd that hasScope expects the scope name and not the method name, but callScope expects the actual method name. (don't change anything yet please. I am writing this comment to be discussed)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did find that weird, but wanted to change the minimum amount possible, and since changing how callScope works would be considered a breaking change, wanted to delegate that to a future PR (if desired).

return $this->callScope([$this->model, 'scope'.ucfirst($method)], $parameters);
}

if (in_array($method, $this->passthru)) {
Expand Down
11 changes: 11 additions & 0 deletions src/Illuminate/Database/Eloquent/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
33 changes: 33 additions & 0 deletions tests/Integration/Database/EloquentModelScopeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Illuminate\Tests\Integration\Database;

use Illuminate\Database\Eloquent\Model;

/**
* @group integration
*/
class EloquentModelScopeTest extends DatabaseTestCase
{
public function testModelHasScope()
{
$model = new TestScopeModel1;

$this->assertTrue($model->hasScope('exists'));
}

public function testModelDoesNotHaveScope()
{
$model = new TestScopeModel1;

$this->assertFalse($model->hasScope('doesNotExist'));
}
}

class TestScopeModel1 extends Model
{
public function scopeExists()
{
return true;
}
}