Skip to content
Merged
Show file tree
Hide file tree
Changes from 15 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
19 changes: 19 additions & 0 deletions src/Illuminate/Database/Eloquent/Attributes/NamedScope.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Illuminate\Database\Eloquent\Attributes;

use Attribute;

#[Attribute(Attribute::TARGET_METHOD)]
class NamedScope
{
/**
* Create a new attribute instance.
*
* @param array|string $classes
* @return void
*/
public function __construct()
{
}
}
28 changes: 27 additions & 1 deletion src/Illuminate/Database/Eloquent/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Illuminate\Contracts\Support\CanBeEscapedWhenCastToString;
use Illuminate\Contracts\Support\Jsonable;
use Illuminate\Database\ConnectionResolverInterface as Resolver;
use Illuminate\Database\Eloquent\Attributes\NamedScope;
use Illuminate\Database\Eloquent\Collection as EloquentCollection;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\Concerns\AsPivot;
Expand All @@ -24,6 +25,7 @@
use JsonException;
use JsonSerializable;
use LogicException;
use ReflectionMethod;
use Stringable;

abstract class Model implements Arrayable, ArrayAccess, CanBeEscapedWhenCastToString, HasBroadcastChannel, Jsonable, JsonSerializable, QueueableEntity, Stringable, UrlRoutable
Expand Down Expand Up @@ -1631,6 +1633,22 @@ public function newPivot(self $parent, array $attributes, $table, $exists, $usin
: Pivot::fromAttributes($parent, $attributes, $table, $exists);
}

protected static function hasAttributedNamedScope(string $scope): ?string
{
if (! method_exists(static::class, $scope)) {
return false;
}

$method = new ReflectionMethod(static::class, $scope);
$attribute = $method->getAttributes(NamedScope::class);

if ($attribute === []) {
return false;
}

return true;
}

/**
* Determine if the model has a given scope.
*
Expand All @@ -1639,7 +1657,7 @@ public function newPivot(self $parent, array $attributes, $table, $exists, $usin
*/
public function hasNamedScope($scope)
{
return method_exists($this, 'scope'.ucfirst($scope));
return method_exists($this, 'scope'.ucfirst($scope)) || static::hasAttributedNamedScope($scope);
}

/**
Expand All @@ -1651,6 +1669,10 @@ public function hasNamedScope($scope)
*/
public function callNamedScope($scope, array $parameters = [])
{
if ($this->hasAttributedNamedScope($scope)) {
return $this->{$scope}(...$parameters);
}

return $this->{'scope'.ucfirst($scope)}(...$parameters);
}

Expand Down Expand Up @@ -2377,6 +2399,10 @@ public function __call($method, $parameters)
*/
public static function __callStatic($method, $parameters)
{
if (static::hasAttributedNamedScope($method)) {
$parameters = [static::query(), ...$parameters];
}

return (new static)->$method(...$parameters);
}

Expand Down
19 changes: 17 additions & 2 deletions tests/Integration/Database/EloquentModelScopeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Illuminate\Tests\Integration\Database;

use Illuminate\Contracts\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Attributes\NamedScope;
use Illuminate\Database\Eloquent\Model;

class EloquentModelScopeTest extends DatabaseTestCase
Expand All @@ -19,12 +21,25 @@ public function testModelDoesNotHaveScope()

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

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

$this->assertTrue($model->hasNamedScope('existsAsWell'));
}
}

class TestScopeModel1 extends Model
{
public function scopeExists()
public function scopeExists(Builder $builder)
{
return $builder;
}

#[NamedScope]
protected function existsAsWell(Builder $builder)
{
return true;
return $builder;
}
}
36 changes: 36 additions & 0 deletions tests/Integration/Database/EloquentNamedScopeAttibuteTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace Illuminate\Tests\Integration\Database;

use Orchestra\Testbench\Attributes\WithMigration;
use Orchestra\Testbench\TestCase;

#[WithMigration]
class EloquentNamedScopeAttibuteTest extends TestCase
{
protected $query = 'select * from "named_scope_users" where "email_verified_at" is not null';

protected function setUp(): void
{
parent::setUp();

$this->markTestSkippedUnless(
$this->usesSqliteInMemoryDatabaseConnection(),
'Requires in-memory database connection',
);
}

public function test_it_can_query_named_scoped_from_the_query_builder()
{
$query = Fixtures\NamedScopeUser::query()->verified(true);

$this->assertSame($this->query, $query->toRawSql());
}

public function test_it_can_query_named_scoped_from_static_query()
{
$query = Fixtures\NamedScopeUser::verified(true);

$this->assertSame($this->query, $query->toRawSql());
}
}
38 changes: 38 additions & 0 deletions tests/Integration/Database/Fixtures/NamedScopeUser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace Illuminate\Tests\Integration\Database\Fixtures;

use Illuminate\Database\Eloquent\Attributes\NamedScope;
use Illuminate\Database\Eloquent\Builder;

class NamedScopeUser extends User
{
/** {@inheritdoc} */
#[\Override]
protected function casts(): array
{
return [
'email_verified_at' => 'datetime',
'password' => 'hashed',
];
}

#[NamedScope]
protected function verified(Builder $builder, bool $email = true)
{
return $builder->when(
$email === true,
fn ($query) => $query->whereNotNull('email_verified_at'),
fn ($query) => $query->whereNull('email_verified_at'),
);
}

public function scopeVerifiedUser(Builder $builder, bool $email = true)
{
return $builder->when(
$email === true,
fn ($query) => $query->whereNotNull('email_verified_at'),
fn ($query) => $query->whereNull('email_verified_at'),
);
}
}