codewithtony / laravel-active-attribute
Laravel Active Attribute
Installs: 919
Dependents: 0
Suggesters: 0
Security: 0
Stars: 3
Watchers: 1
Forks: 0
Open Issues: 0
Type:laravel
pkg:composer/codewithtony/laravel-active-attribute
Requires
- laravel/framework: >=5.5
This package is not auto-updated.
Last update: 2025-10-26 11:45:01 UTC
README
Laravel Active Attribute adds withInactive and onlyInactive functionality to an Eloquent Model.
This can be used to automatically hide inactive records unless otherwise desired. A better solution might be the use Soft Deletes but there are a number of reasons why you might want to use an active field.
This can also serve as a good example of how to do similar things to use Scopes and Macros in Laravel to manipulate queries at a higher level.
Install
composer require codewithtony/laravel-active-attribute
Usage
Add the HasActiveAttribute to any Eloquent Model that has an ->active field that is a 1/0 boolean
class Thing
{
Use HasActiveAttribute;
...
}
After that you can use it as follows
Thing::all(); // only records where $model->active = 1
// or
Thing::withInactive()->all(); // records where $model->active = 1 OR 0
// or
Thing::onlyInactive()->all(); // only records where $model->active = 0
Customize Active Feild
By default active is the default field that this will look at, to customize it you can set the constant ACTIVE. For example, if the field is named is_active.
class Thing
{
const ACTIVE = 'is_active';
Use HasActiveAttribute;
...
}