Skip to content

Commit

Permalink
Merge branch 'feature/has-scope' of https://github.com/alexbowers/fra…
Browse files Browse the repository at this point in the history
…mework into alexbowers-feature/has-scope
  • Loading branch information
taylorotwell committed May 1, 2020
2 parents f69ad90 + bbdc776 commit b706fe6
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 2 deletions.
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)) {
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;
}
}

0 comments on commit b706fe6

Please sign in to comment.