GithubHelp home page GithubHelp logo

astralo / laravel-local-class-scope Goto Github PK

View Code? Open in Web Editor NEW

This project forked from mpyw/laravel-local-class-scope

0.0 1.0 0.0 13 KB

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

License: MIT License

PHP 100.00%

laravel-local-class-scope's Introduction

Laravel Local Class Scope Build Status Coverage Status Scrutinizer Code Quality

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

Requirements

  • PHP: 7.1+
  • Laravel: 5.5+

Installing

composer require mpyw/laravel-local-class-scope

Usage

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

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

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

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

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;
}

laravel-local-class-scope's People

Contributors

mpyw avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.