Automatically convert your Eloquent scopes to boolean check methods.
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();
You can install the package via composer:
composer require beyondcode/laravel-scope-checks
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);
composer test
Please see CHANGELOG for more information on what has changed recently.
Please see CONTRIBUTING for details.
If you discover any security related issues, please email marcel@beyondco.de instead of using the issue tracker.
The MIT License (MIT). Please see License File for more information.