Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for Eloquent Scope extensions #1532

Open
neoighodaro opened this issue Mar 10, 2024 · 1 comment
Open

Support for Eloquent Scope extensions #1532

neoighodaro opened this issue Mar 10, 2024 · 1 comment

Comments

@neoighodaro
Copy link

Summary

Support for extensions from Laravel's Eloquent scope extend method: https://www.stephenlewis.me/blog/overriding-eloquent-global-scopes/

<?php

namespace App\Scopes;

use Illuminate\Database\Eloquent\Scope;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Builder;

class AgeScope implements Scope
{
    /**
     * Restrict results to users aged 18 or over.
     *
     * @param Builder $builder
     * @param Model  $model
     */
    public function apply(Builder $builder, Model $model)
    {
        $builder->where('age', '>=', 18);
    }

    /**
     * Extend the query builder with the needed functions.
     *
     * @param Builder $builder
     */
    public function extend(Builder $builder)
    {
        $builder->macro('withYouths', function (Builder $builder) {
            return $builder->withoutGlobalScope($this);
        });
    }
}```

```php
User::withYouths()->get();

Currently it suffices to add the @method static Builder|User withYouths() declaration in your model.

@mfn
Copy link
Collaborator

mfn commented Mar 10, 2024

Can you check the hooks if you can write a custom code for it?

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

No branches or pull requests

2 participants