Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/mpyw/laravel-local-class-scope
A tiny macro that reuse a global scope class as a local scope
https://github.com/mpyw/laravel-local-class-scope
eloquent laravel macro php
Last synced: 18 days ago
JSON representation
A tiny macro that reuse a global scope class as a local scope
- Host: GitHub
- URL: https://github.com/mpyw/laravel-local-class-scope
- Owner: mpyw
- License: mit
- Created: 2019-05-04T12:08:29.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2023-03-13T11:48:30.000Z (over 1 year ago)
- Last Synced: 2024-10-13T13:07:47.996Z (about 1 month ago)
- Topics: eloquent, laravel, macro, php
- Language: PHP
- Homepage:
- Size: 37.1 KB
- Stars: 23
- Watchers: 3
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
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) [![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/mpyw/laravel-local-class-scope/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/mpyw/laravel-local-class-scope/?branch=master)
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
- PHP: `^8.0`
- Laravel: `^9.0 || ^10.0`## Installing
```bash
composer require mpyw/laravel-local-class-scope
```## Usage
### Simple Scope
```php
class ActiveScope implements Scope
{
public function apply(Builder $query, Model $model): void
{
$query->where('active', true);
}
}
``````php
User::scoped(ActiveScope::class)->get();
``````php
User::scoped(new ActiveScope())->get();
```### Scope that takes arguments
```php
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);
}
}
``````php
User::scoped(AgeScope::class, '>', 18)->get();
``````php
User::scoped(new AgeScope('>', 18))->get();
```### Combination
```php
User::scoped(ActiveScope::class)->scoped(AgeScope::class, '>', 18)->get();
```### Re-define as a local method scope
```php
class User extends Model
{
public function scopeActive(Builder $query): Builder
{
return $this->scoped(ActiveScope::class);
}
}
```### Share local method re-definition via trait
```php
trait ScopesActive
{
public function scopeActive(Builder $query): Builder
{
return $this->scoped(ActiveScope::class);
}
}
``````php
class User extends Model
{
use ScopesActive;
}
``````php
class Admin extends Model
{
use ScopesActive;
}
```