A Laravel package that simplifies status management for Eloquent models.
- β
HasActiveScope
Trait: Automatically filters models with active status. - β Admin Bypass: Admin users can see all models, including inactive ones.
- β
Helper Methods:
$model->status->isActive()
and$model->status->isInactive()
. - β
Dynamic Configuration: Define custom statuses & column names via
.env
. - β
Installation Command:
php artisan model-status:install
for easy setup. - β PHP 8.3 Support.
You can install the package via Composer:
composer require thefeqy/laravel-model-status
php artisan model-status:install
This will:
- Publish the config file (
config/model-status.php
). - Set up required environment variables in
.env
and.env.example
. - Ensure your project is ready to use the package.
You can customize the package settings in:
π config/model-status.php
return [
'column_name' => env('MODEL_STATUS_COLUMN', 'status'),
'default_value' => env('MODEL_STATUS_ACTIVE', 'active'),
'inactive_value' => env('MODEL_STATUS_INACTIVE', 'inactive'),
'admin_detector' => function () {
return auth()->check() && auth()->user()->is_admin;
},
];
Instead of modifying config/model-status.php
, you can override values in .env:
π .env
MODEL_STATUS_COLUMN=state
MODEL_STATUS_ACTIVE=enabled
MODEL_STATUS_INACTIVE=disabled
Now, the package will automatically adapt to your setup.
To enable status management in a model:
use Thefeqy\ModelStatus\Traits\HasActiveScope;
class Product extends Model
{
use HasActiveScope;
protected $fillable = ['name'];
}
Now, inactive models are automatically excluded from queries.
β Get Active Models (Default Behavior)
$activeProducts = Product::all(); // Returns only active products
β Get All Models (Including Inactive)
$allProducts = Product::withoutActive()->get();
β Manually Activating / Deactivating a Model
$product = Product::find(1);
$product->activate(); // Set status to "active"
$product->deactivate(); // Set status to "inactive"
β Checking a Model's Status
if ($product->status->isActive()) {
echo "Product is active!";
}
if ($product->status->isInactive()) {
echo "Product is inactive!";
}
The package includes the EnsureAuthenticatedUserIsActive
middleware, which enforces that only users with an active status can access certain routes.
Instead of registering a string alias for the middleware, you can reference it by class name in your route definition:
use Illuminate\Support\Facades\Route;
use Thefeqy\ModelStatus\Middleware\EnsureAuthenticatedUserIsActive;
Route::middleware(['auth', EnsureAuthenticatedUserIsActive::class])->group(function () {
Route::get('/dashboard', function () {
return 'Welcome to your dashboard!';
});
});
By default, admin users can see inactive models.
This behavior is controlled in config/model-status.php
:
'admin_detector' => function () {
return auth()->check() && auth()->user()->is_admin;
},
If you need a different column name or status values, update π .env
:
MODEL_STATUS_COLUMN=state
MODEL_STATUS_ACTIVE=enabled
MODEL_STATUS_INACTIVE=disabled
Now, models will use:
$table->string('state', 10)->default('enabled');
instead of:
$table->string('status', 10)->default('active');
Run tests using Pest PHP:
composer test
or
vendor/bin/phpunit
If you discover a security vulnerability, please report it via email: π© thefeqy@gmail.com
Want to improve this package? Check out CONTRIBUTING for contribution guidelines.
This package is open-source software licensed under the MIT License.