wowe / laravel-eloquent-scopes
Some general scopes for Eloquent
v3.0.0
2017-05-19 01:43 UTC
Requires
- illuminate/database: 5.1.*
Requires (Dev)
- orchestra/testbench: 3.1.*
- phpunit/phpunit: ~4.5.0
README
This package includes some useful global scopes for Laravel Eloquent.
Installation
Traits
Usage
Release Notes
Version Compatibility
License
Installation
Via Composer
composer require wowe/laravel-eloquent-scopes
Traits
ActivatableTrait
When this trait is added to an Eloquent model, it will only return rows which have the active
field set to true
.
If you want to use a field with a different name than active
, set a constant called ACTIVE
on the Eloquent model to whichever name you would like to use.
The following methods are included with the trait:
deactivate
: sets theactive
field tofalse
.activate
: sets theactive
field totrue
.withInactive
: will include all entries in results regardless of the value of theactive
field.onlyInactive
: will include only those entries withactive
fields set tofalse
.
Usage
Add Trait to Model
Add the ActivatableTrait
to an Eloquent model.
use Wowe\Eloquent\Scopes\ActivatableTrait; class Test extends \Eloquent { use ActivatableTrait; const ACTIVE = 'is_active'; }
Query Model
// Get all entries that are active Test::all(); // Get a single entry only if active Test::find(1); // Get all entries Test::withInactive()->get(); // Get all inactive entries Test::inactiveOnly()->get();
Version Compatibility
License
This package is open-sourced software licensed under the MIT license