Not possible to combine with custom scope
philharmonie opened this issue · 1 comments
philharmonie commented
Describe the bug
Combining an eloquent query using role()
function with a custom scope ignores the role selection.
Versions
spatie/laravel-permission 5.10.2
laravel/framework v10.20.0
PHP version: 8.2.9
Database version:
mysql 8.0.33
To Reproduce
Steps to reproduce the behavior:
class User extends Authenticatable
{
use HasApiTokens;
use HasFactory;
use Notifiable;
use HasRoles;
protected $fillable = [
'name',
'email',
'password',
'remember_token',
'tarif',
'active',
'email_verified_at',
];
public function scopeActive()
{
return $this->where('active', true);
}
Here is my example code and/or tests showing the problem in my app:
Example
User::role('Admin')->toSql();
gives
select * from `users` where exists (select * from `roles` inner join `model_has_roles` on `roles`.`id` = `model_has_roles`.`role_id` where `users`.`id` = `model_has_roles`.`model_id` and `model_has_roles`.`model_type` = ? and `roles`.`id` in (?)) and `users`.`deleted_at` is null
User::role('Admin')->active()->toSql();
gives
select * from `users` where `active` = ? and `users`.`deleted_at` is null
Expected behavior
I expected to see all Admins that are active.
Environment (please complete the following information, because it helps us investigate better):
- OS: MacOS with Herd (all latesst)
Current workaround:
$roleUsers = User::role('Admin')->pluck('id');
$activeRoleUsers = User::active()->whereIn('id', $roleUsers)->get();
philharmonie commented
public function scopeActive($query)
{
return $query->where('active', true);
}