<?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.
Summary
Support for extensions from Laravel's Eloquent scope
extend
method: https://www.stephenlewis.me/blog/overriding-eloquent-global-scopes/Currently it suffices to add the
@method static Builder|User withYouths()
declaration in your model.