PHPackages                             mpyw/laravel-local-class-scope - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. mpyw/laravel-local-class-scope

ActiveLibrary[Database &amp; ORM](/categories/database)

mpyw/laravel-local-class-scope
==============================

A tiny macro that reuse a global scope class as a local scope

v2.3.0(3w ago)24112.8k↓12.8%4MITPHPPHP ^8.2CI passing

Since May 4Pushed 3w ago1 watchersCompare

[ Source](https://github.com/mpyw/laravel-local-class-scope)[ Packagist](https://packagist.org/packages/mpyw/laravel-local-class-scope)[ RSS](/packages/mpyw-laravel-local-class-scope/feed)WikiDiscussions master Synced 3d ago

READMEChangelog (6)Dependencies (13)Versions (12)Used By (0)

Laravel Local Class Scope [![Build Status](https://github.com/mpyw/laravel-local-class-scope/actions/workflows/ci.yml/badge.svg?branch=master)](https://github.com/mpyw/laravel-local-class-scope/actions) [![Coverage Status](https://camo.githubusercontent.com/ac52f7055d2be33d2ad1daf10770f2eb132ef2d8962eae103c3b64fc4e1755a4/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f6d7079772f6c61726176656c2d6c6f63616c2d636c6173732d73636f70652f62616467652e7376673f6272616e63683d6d6173746572)](https://coveralls.io/github/mpyw/laravel-local-class-scope?branch=master)
===============================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================

[](#laravel-local-class-scope--)

A tiny macro that reuse a global scope class as a local scope.

The idea is from: [\[Proposal\] Local query scopes as classes · Issue #636 · laravel/ideas](https://github.com/laravel/ideas/issues/636)

Requirements
------------

[](#requirements)

- PHP: `^8.2`
- Laravel: `^11.0 || ^12.0 || ^13.0 || ^14.0`

Note

Older versions have outdated dependency requirements. If you cannot prepare the latest environment, please refer to past releases.

Installing
----------

[](#installing)

```
composer require mpyw/laravel-local-class-scope
```

Usage
-----

[](#usage)

### Simple Scope

[](#simple-scope)

```
class ActiveScope implements Scope
{
    public function apply(Builder $query, Model $model): void
    {
        $query->where('active', true);
    }
}
```

```
User::scoped(ActiveScope::class)->get();
```

```
User::scoped(new ActiveScope())->get();
```

### Scope that takes arguments

[](#scope-that-takes-arguments)

```
class AgeScope implements Scope
{
    protected $parameters;

    public function __construct(...$parameters)
    {
        $this->parameters = $parameters;
    }

    public function apply(Builder $query, Model $model): void
    {
        $query->where('age', ...$this->parameters);
    }
}
```

```
User::scoped(AgeScope::class, '>', 18)->get();
```

```
User::scoped(new AgeScope('>', 18))->get();
```

### Combination

[](#combination)

```
User::scoped(ActiveScope::class)->scoped(AgeScope::class, '>', 18)->get();
```

### Re-define as a local method scope

[](#re-define-as-a-local-method-scope)

```
class User extends Model
{
    public function scopeActive(Builder $query): Builder
    {
        return $this->scoped(ActiveScope::class);
    }
}
```

### Share local method re-definition via trait

[](#share-local-method-re-definition-via-trait)

```
trait ScopesActive
{
    public function scopeActive(Builder $query): Builder
    {
        return $this->scoped(ActiveScope::class);
    }
}
```

```
class User extends Model
{
    use ScopesActive;
}
```

```
class Admin extends Model
{
    use ScopesActive;
}
```

PHPStan extension
-----------------

[](#phpstan-extension)

You can use the PHPStan extension to get better static analysis support for the `scoped` method.

To enable the extension, you need to include it in the `phpstan.neon` configuration file as follows:

```
includes:
    - vendor/mpyw/laravel-local-class-scope/extension.neon
    - vendor/larastan/larastan/extension.neon
```

Important

If you are using [Larastan](https://github.com/larastan/larastan) (as is typical), you must include the extension of this package **before** Larastan's extension.

###  Health Score

62

—

FairBetter than 99% of packages

Maintenance95

Actively maintained with recent releases

Popularity42

Moderate usage in the ecosystem

Community13

Small or concentrated contributor base

Maturity79

Established project with proven stability

 Bus Factor1

Top contributor holds 93.1% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~259 days

Recently: every ~357 days

Total

11

Last Release

22d ago

Major Versions

v1.0.2 → v2.0.02019-10-03

PHP version history (5 changes)v1.0.0PHP ^7.1

v2.0.1PHP ^7.1 || ^8.0

v2.1.0PHP ^7.3 || ^8.0

v2.1.2PHP ^8.0

v2.2.0PHP ^8.2

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/1351893?v=4)[mpyw](/maintainers/mpyw)[@mpyw](https://github.com/mpyw)

---

Top Contributors

[![mpyw](https://avatars.githubusercontent.com/u/1351893?v=4)](https://github.com/mpyw "mpyw (27 commits)")[![hexium310](https://avatars.githubusercontent.com/u/10758173?v=4)](https://github.com/hexium310 "hexium310 (1 commits)")[![ngmy](https://avatars.githubusercontent.com/u/864041?v=4)](https://github.com/ngmy "ngmy (1 commits)")

---

Tags

eloquentlaravelmacrophplaraveldatabaseeloquentquerybuilderilluminatemacroscope

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Type Coverage Yes

### Embed Badge

![Health badge](/badges/mpyw-laravel-local-class-scope/health.svg)

```
[![Health](https://phpackages.com/badges/mpyw-laravel-local-class-scope/health.svg)](https://phpackages.com/packages/mpyw-laravel-local-class-scope)
```

###  Alternatives

[mongodb/laravel-mongodb

A MongoDB based Eloquent model and Query builder for Laravel

7.1k8.4M96](/packages/mongodb-laravel-mongodb)[kirschbaum-development/eloquent-power-joins

The Laravel magic applied to joins.

1.6k32.6M46](/packages/kirschbaum-development-eloquent-power-joins)[psalm/plugin-laravel

Psalm plugin for Laravel

3355.3M346](/packages/psalm-plugin-laravel)[yajra/laravel-oci8

Oracle DB driver for Laravel via OCI8

8793.2M25](/packages/yajra-laravel-oci8)[glushkovds/phpclickhouse-laravel

Adapter of the most popular library https://github.com/smi2/phpClickHouse to Laravel

2051.5M2](/packages/glushkovds-phpclickhouse-laravel)[reedware/laravel-relation-joins

Adds the ability to join on a relationship by name.

2121.3M18](/packages/reedware-laravel-relation-joins)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
