Skip to content
This repository has been archived by the owner on Jul 16, 2021. It is now read-only.

[Proposal] Apply Global scope to Eloquent Builder as a query #2442

Open
ObaydaAlesawi opened this issue Dec 13, 2020 · 0 comments
Open

[Proposal] Apply Global scope to Eloquent Builder as a query #2442

ObaydaAlesawi opened this issue Dec 13, 2020 · 0 comments

Comments

@ObaydaAlesawi
Copy link

Since we want to separate our business logic and keep the builder class clean as much as possible, we decided to move the large builder functions to separate Scope, and apply it to the query when needed. see similar proposals #636 , #1164.

Proposal:

Scope Class

class GlobalScope implements Scope
{
    public function apply(Builder $builder, Model $model)
    {
       return $builder->someQuery();
    }
}

Apply function in Builder class

    public function apply(Scope $scope)
    {
        $scope->apply($this, $this->getModel());

        return $this;
    }

Usage

Model::query()->apply(new GlobalScope())->get();

We think that could be a good idea to be implemented in Laravel, what do you think?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant