Laravel Model Status
Laravel Model Status stats
- Downloads
- 3
- Stars
- 8
- Open Issues
- 0
- Forks
- 0
A Laravel package to automate adding configurable status columns to models and migrations, and enforce user activity through middleware.
Laravel Model Status
A Laravel package to automate adding configurable status columns to models and migrations. This package provides an easy-to-use HasActiveScope trait, a Status enum, middleware for ensuring active users, and a custom Artisan command to streamline your workflow.
Features
- Add a configurable
statuscolumn to models and migrations. - Automatically handle global scopes for active statuses.
- Enforce user activity with the
EnsureAuthenticatedUserIsActivemiddleware. - Activate or deactivate models with simple methods.
- Support dynamic configuration for column name, default value, and length.
- Includes a
make:model-statuscommand.
Installation
You can install the package via Composer:
composer require thefeqy/laravel-model-status
Optionally, you can publish the configuration file:
php artisan vendor:publish --tag=config
Usage
Using the Trait
To use the HasActiveScope trait, include it in your model:
use Thefeqy\ModelStatus\Traits\HasActiveScope; class ExampleModel extends Model{ use HasActiveScope; protected $fillable = ['name'];}
Creating Models with status
You can use the custom Artisan command to create models and migrations with a status column automatically:
php artisan make:model-status Example
This will:
- Create a model named
Exampleand automatically useHaveActiveScope. - Add a
statuscolumn to the generated migration file with the configuration values.
Querying Models
Retrieve Active Models (Default Behavior)
$activeModels = ExampleModel::all();
Include Inactive Models
$allModels = ExampleModel::withoutActive()->get();
Middleware Usage
The package includes the EnsureAuthenticatedUserIsActive middleware, which enforces that only users with an active status can access certain routes.
Add Middleware to 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!'; });}); Route::get('/login', function () { return 'Login page';})->name('login');
Behavior
- If the authenticated user's status is not active:
- The user will be logged out.
- A
403 Forbiddenresponse will be returned with the message:
This account is suspended. Please contact the administrator.
Example Workflow
1- Use the make:model-status command to create a model with a status column and include the HasActiveScope trait:
php artisan make:model-status Company
2- Update the database schema to include a status column in the companies table (if not already present):
php artisan migrate
- Protect your routes using the middleware to ensure only active users can access them:
use App\Models\Company;use Thefeqy\ModelStatus\Middleware\EnsureAuthenticatedUserIsActive; Route::middleware(['auth', EnsureAuthenticatedUserIsActive::class])->group(function () { Route::get('/companies', function () { return view('companies', ['companies' => Company::get()]); });});
- Test the functionality:
- try to access the
companiesroute to make sure that only active companies are included in the response
Activating and Deactivating Models
The HasActiveScope trait provides two methods: activate and deactivate. These methods dynamically use the configuration values for the status column to update the mode status.
Activate a Model
$model = ExampleModel::find(1);if ($model->activate()) { echo "Model activated successfully!";} else { echo "Failed to activate model.";}
Deactivate a Model
$model = ExampleModel::find(1);if ($model->deactivate()) { echo "Model deactivated successfully!";} else { echo "Failed to deactivate model.";}
Configuration
Edit the config/model-status.php file to customize the column name, default value, inactive value, and length:
return [ 'column_name' => 'status', // The name of the status column 'default_value' => 'active', // The value for the "active" state 'inactive_value' => 'inactive', // The value for the "inactive" state 'column_length' => 10, // The maximum length of the status column];
If you want to use a different column name (e.g., account_status), update the column_name value in config/model-status.php and ensure the corresponding database schema matches.
Example Configuration Use Case
- If you change the
column_nametostatein the config, the migration will automatically generate:
$table->string('state', 10)->default('active');
- The global scope and the
HasActiveScopetrait will also dynamically adapt to usestateinstead ofstatus.
Contributing
Contributions are welcome! https://github.com/thefeqy/laravel-model-status Please follow these steps:
- Fork the repository.
- Create a new branch:
git checkout -b feature/your-feature
- Commit your changes:
git commit -m "Add your feature"
- Push to the branch:
git push origin feature/your-feature
- Open a pull request.
License
The MIT License (MIT). Please see License File for more information.