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

Global scope applied twice when using query builder paginate method. #42515

Closed
hekin1 opened this issue May 25, 2022 · 2 comments
Closed

Global scope applied twice when using query builder paginate method. #42515

hekin1 opened this issue May 25, 2022 · 2 comments

Comments

@hekin1
Copy link

hekin1 commented May 25, 2022

  • Laravel Version: 8.X.X and 9.X.X
  • PHP Version: 7.X and 8.X
  • Database Driver & Version: All

Description:

When adding a global scope to the boot-method of a model and also using the query builder paginate for that same model, then the global scope is executed twice.

Steps To Reproduce:

  1. Add a global scope to your model:
protected static function boot()
{
    parent::boot();
    static::addGlobalScope(new MyGlobalScope());
}
  1. Run a query with paginate:
MyModel::where('id', '>', 0)->paginate(15);
  1. Result: MyGlobalScope is executed twice.

Execution backtrace #1:

1 => 
    'file' => '/var/www/html/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php',
    'function' => 'callScope',
    'class' => 'Illuminate\\Database\\Eloquent\\Builder',
2 => 
    'file' => '/var/www/html/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php',
    'function' => 'applyScopes',
    'class' => 'Illuminate\\Database\\Eloquent\\Builder',
3 => 
    'file' => '/var/www/html/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php',
    'function' => 'get',
    'class' => 'Illuminate\\Database\\Eloquent\\Builder',
4 => 
    'file' => '/var/www/html/app/MyTestFile.php',
    'function' => 'paginate',
    'class' => 'Illuminate\\Database\\Eloquent\\Builder',

Execution backtrace #2:

1 => 
    'file' => '/var/www/html/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php',
    'function' => 'callScope',
    'class' => 'Illuminate\\Database\\Eloquent\\Builder',
2 => 
    'file' => '/var/www/html/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php',
    'function' => 'applyScopes',
    'class' => 'Illuminate\\Database\\Eloquent\\Builder',
3 => 
    'file' => '/var/www/html/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php',
    'function' => 'toBase',
    'class' => 'Illuminate\\Database\\Eloquent\\Builder',
4 => 
    'file' => '/var/www/html/app/MyTestFile.php',
    'function' => 'paginate',
    'class' => 'Illuminate\\Database\\Eloquent\\Builder',
@driesvints
Copy link
Member

Hey there,

Can you first please try one of the support channels below? If you can actually identify this as a bug, feel free to open up a new issue with a link to the original one and we'll gladly help you out.

Thanks!

@hekin1
Copy link
Author

hekin1 commented May 25, 2022

Hey there,

Can you first please try one of the support channels below? If you can actually identify this as a bug, feel free to open up a new issue with a link to the original one and we'll gladly help you out.

* [Laracasts Forums](https://laracasts.com/discuss)

* [Laravel.io Forums](https://laravel.io/forum)

* [StackOverflow](https://stackoverflow.com/questions/tagged/laravel)

* [Discord](https://discordapp.com/invite/KxwQuKb)

* [Larachat](https://larachat.co)

* [IRC](https://web.libera.chat/?nick=laravelnewbie&channels=#laravel)

Thanks!

Sorry, I cant do that. This is a bug as you can see from the stack-trace, but I will not force you to fix it, that is up to you.

Have a nice day!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants