Thanks to visit codestin.com
Credit goes to github.com

Skip to content

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

Closed
@hekin1

Description

@hekin1
  • 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',

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions