LaravelPackages.net
Acme Inc.
Toggle sidebar
akr4m/scoping

This scopes allow you to add constraints to all queries for a given model. Filter your data easily.

99
1
v1.0.2
About akr4m/scoping

akr4m/scoping is a Laravel package for this scopes allow you to add constraints to all queries for a given model. filter your data easily.. It currently has 1 GitHub stars and 99 downloads on Packagist (latest version v1.0.2). Install it with composer require akr4m/scoping. Discover more Laravel packages by akr4m or browse all Laravel packages to compare alternatives.

Last updated

Filter Scoping your Queries

This scopes allow you to add constraints to all queries for a given model. Filter your data easily.

Installation

Simply add the package to your composer.json file and run composer update.

composer require akr4m/scoping

Usage

Add the trait to your model and your search rules.

use akr4m\scoping\Traits\CanBeScoped;

class Post extends Model
{
    use CanBeScoped;
}

Add scopes in abcController.php like this

public function __invoke(Request $request)
{
    $posts  = App\Post::withScopes($this->scopes())->get();
}

protected function scopes()
{
    return [
        // Must declare the `Scope` files
        'topic' => new TopicScope(),
        'month' => new MonthScope(),
        'year' => new YearScope(),
    ];
}

TopicScope.php file would be like this

use akr4m\scoping\Scoping\Contracts\Scope;
use Illuminate\Database\Eloquent\Builder;

class TopicScope implements Scope
{
    public function apply(Builder $builder, $value)
    {
        return $builder
            ->whereHas('topics', function ($builder) use ($topic) {
                $builder->where('slug', $value);
            });
    }
}

Star History Chart