-
Notifications
You must be signed in to change notification settings - Fork 11.2k
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
[12.x] Add NamedScope attribute #54450
base: master
Are you sure you want to change the base?
Conversation
Drafting pending for @crynobone - please mark as ready for review once reviewed. |
@shaedrich using |
I can make it |
return null; | ||
} | ||
|
||
$method = new ReflectionMethod($this, $scope); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you think we need to check if $method
return false
for incorrect usage such as abstract
method or public function
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In lines 1638 – 1640, we test if the method exists—isn't that enough, meaning do we care about how the method is implemented?
@@ -27,4 +35,10 @@ public function scopeExists() | |||
{ | |||
return true; | |||
} | |||
|
|||
#[NamedScope] | |||
public function existsAsWell() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Need to also test if child model can utilise attribute named scope defined in parent model.
method need to be updated to protected and contains parameter as example to real usage
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm afraid, I can't follow you? Can you elaborate on that?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Correct 👍🏻
Signed-off-by: Mior Muhammad Zaki <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Given the following example:
<?php
namespace App\Models;
// use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Contracts\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Attributes\NamedScoped;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
class User extends Authenticatable
{
/** @use HasFactory<\Database\Factories\UserFactory> */
use HasFactory, Notifiable;
/**
* The attributes that are mass assignable.
*
* @var list<string>
*/
protected $fillable = [
'name',
'email',
'password',
];
/**
* The attributes that should be hidden for serialization.
*
* @var list<string>
*/
protected $hidden = [
'password',
'remember_token',
];
/**
* Get the attributes that should be cast.
*
* @return array<string, string>
*/
protected function casts(): array
{
return [
'email_verified_at' => 'datetime',
'password' => 'hashed',
];
}
#[NamedScoped]
public function verified(Builder $builder, bool $email = true)
{
return $builder->when(
$email === true,
fn ($query) => $query->whereNotNull('email_verified_at'),
fn ($query) => $queryu->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) => $queryu->whereNull('email_verified_at'),
);
}
}
Accessing named scope via query builder:
Accessing named scope statically:
Converting public function verified
to protected function verified
Signed-off-by: Mior Muhammad Zaki <[email protected]>
Added failing tests based on above finding |
Signed-off-by: Mior Muhammad Zaki <[email protected]>
Signed-off-by: Mior Muhammad Zaki <[email protected]>
Signed-off-by: Mior Muhammad Zaki <[email protected]>
Wouldnt #[LocalScope] be a better name for the attribute? |
|
Yeah, I thought about this, too.
@taylorotwell @crynobone What do you think? |
to comply with PSR-4
Instead of
you can now write
similar to #40022
and
Complements #50034
FAQs
What if I already have a method that has the same name as a scope?
Your application will not be broken because the method does not have the
NamedScope
attribute, which did not exist before this pull request.Will the old, multi-method approach of defining local scopes go away?
No. This is just an alternative approach.