All Projects → beyondcode → laravel-scope-checks

beyondcode / laravel-scope-checks

Licence: MIT license
Automatically convert your Eloquent scopes to boolean check methods.

Programming Languages

PHP
23972 projects - #3 most used programming language

Laravel Scope Checks

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 [email protected] instead of using the issue tracker.

Credits

License

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

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].