beyondcode/laravel-scope-checks

This package is abandoned and no longer maintained. No replacement package was suggested.

Automatically convert your Eloquent scopes to boolean check methods.

1.0.0 2020-02-06 20:11 UTC

This package is auto-updated.

Last update: 2021-04-17 12:29:23 UTC


README

Automatically convert your Eloquent scopes to boolean check methods.

Latest Version on Packagist Build Status Quality Score Total Downloads

This package allows you to automatically call all your eloquent model scope methods as checks.

class User extends Model
{
    use HasScopeChecks;
    
    public function scopeActive($query)
    {
        $query->where('active', true);
    }
    
    public function scopeActive($query)
    {
        return $query->where('active', 1);
    }
}

// Now you can call your scope check using:
$user->isActive();

Installation

You can install the package via composer:

composer require beyondcode/laravel-scope-checks

Usage

All you need to do to be able to call your scopes as check methods is add the HasScopeChecks trait to your eloquent model.

use \BeyondCode\LaravelScopeChecks\HasScopeChecks;

class Post {
    use HasScopeChecks;
    
    public function scopePublished($query)
    {
        return $query->where('active', 1);
    }
    
    public function scopeMinimumRating($query, $rating = 5)
    {
        return $query->where('rating', '>=', $rating);
    }
}

You can either call your check methods using the is or the has naming prefix on your model instances. For example:

$post->isPublished();
$post->hasMinimumRating();

When you make use of dynamic scopes (like the scopeRating method), you can also pass the additional scope parameters to the check methods:

$post->isMinimumRating(1);
$post->hasMinimumRating(1);

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email marcel@beyondco.de instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.