{"id":16675824,"url":"https://github.com/mpyw/laravel-local-class-scope","last_synced_at":"2025-04-09T08:11:31.189Z","repository":{"id":45681167,"uuid":"184889884","full_name":"mpyw/laravel-local-class-scope","owner":"mpyw","description":"A tiny macro that reuse a global scope class as a local scope","archived":false,"fork":false,"pushed_at":"2025-03-05T10:38:27.000Z","size":41,"stargazers_count":24,"open_issues_count":0,"forks_count":3,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-01T23:37:07.829Z","etag":null,"topics":["eloquent","laravel","macro","php"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mpyw.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-05-04T12:08:29.000Z","updated_at":"2025-03-05T10:36:36.000Z","dependencies_parsed_at":"2024-10-27T12:00:21.192Z","dependency_job_id":null,"html_url":"https://github.com/mpyw/laravel-local-class-scope","commit_stats":{"total_commits":24,"total_committers":3,"mean_commits":8.0,"dds":0.08333333333333337,"last_synced_commit":"72ed8b332a5ed1c410ef5820801ff9bd688ab692"},"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mpyw%2Flaravel-local-class-scope","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mpyw%2Flaravel-local-class-scope/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mpyw%2Flaravel-local-class-scope/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mpyw%2Flaravel-local-class-scope/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mpyw","download_url":"https://codeload.github.com/mpyw/laravel-local-class-scope/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247999864,"owners_count":21031046,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["eloquent","laravel","macro","php"],"created_at":"2024-10-12T13:08:02.972Z","updated_at":"2025-04-09T08:11:31.164Z","avatar_url":"https://github.com/mpyw.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# 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://coveralls.io/repos/github/mpyw/laravel-local-class-scope/badge.svg?branch=master)](https://coveralls.io/github/mpyw/laravel-local-class-scope?branch=master)\n\nA tiny macro that reuse a global scope class as a local scope.\n\nThe idea is from: [[Proposal] Local query scopes as classes · Issue #636 · laravel/ideas](https://github.com/laravel/ideas/issues/636)\n\n## Requirements\n\n- PHP: `^8.2`\n- Laravel: `^11.0 || ^12.0`\n\n\u003e [!NOTE]\n\u003e Older versions have outdated dependency requirements. If you cannot prepare the latest environment, please refer to past releases.\n\n## Installing\n\n```bash\ncomposer require mpyw/laravel-local-class-scope\n```\n\n## Usage\n\n### Simple Scope\n\n```php\nclass ActiveScope implements Scope\n{\n    public function apply(Builder $query, Model $model): void\n    {\n        $query-\u003ewhere('active', true);\n    }\n}\n```\n\n```php\nUser::scoped(ActiveScope::class)-\u003eget();\n```\n\n```php\nUser::scoped(new ActiveScope())-\u003eget();\n```\n\n### Scope that takes arguments\n\n```php\nclass AgeScope implements Scope\n{\n    protected $parameters;\n\n    public function __construct(...$parameters)\n    {\n        $this-\u003eparameters = $parameters;\n    }\n\n    public function apply(Builder $query, Model $model): void\n    {\n        $query-\u003ewhere('age', ...$this-\u003eparameters);\n    }\n}\n```\n\n```php\nUser::scoped(AgeScope::class, '\u003e', 18)-\u003eget();\n```\n\n```php\nUser::scoped(new AgeScope('\u003e', 18))-\u003eget();\n```\n\n### Combination\n\n```php\nUser::scoped(ActiveScope::class)-\u003escoped(AgeScope::class, '\u003e', 18)-\u003eget();\n```\n\n### Re-define as a local method scope\n\n```php\nclass User extends Model\n{\n    public function scopeActive(Builder $query): Builder\n    {\n        return $this-\u003escoped(ActiveScope::class);\n    }\n}\n```\n\n### Share local method re-definition via trait\n\n```php\ntrait ScopesActive\n{\n    public function scopeActive(Builder $query): Builder\n    {\n        return $this-\u003escoped(ActiveScope::class);\n    }    \n}\n```\n\n```php\nclass User extends Model\n{\n    use ScopesActive;\n}\n```\n\n```php\nclass Admin extends Model\n{\n    use ScopesActive;\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmpyw%2Flaravel-local-class-scope","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmpyw%2Flaravel-local-class-scope","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmpyw%2Flaravel-local-class-scope/lists"}