PHP code example of mpyw / laravel-local-class-scope

1. Go to this page and download the library: Download mpyw/laravel-local-class-scope library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

mpyw / laravel-local-class-scope example snippets


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();

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();

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

class User extends Model
{
    public function scopeActive(Builder $query): Builder
    {
        return $this->scoped(ActiveScope::class);
    }
}

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