Simple model state management package for Laravel. This package allows you to define and store model states in a state model with caching and updating.
- Define states for your Eloquent models
- Store and manage states in a separate state model
- Cache and update states efficiently
- Integration with Blade for easy state formatting
- Facade for easy state management
- Install the package via Composer:
composer require dimonka2/flatstate
- Publish the configuration and migration files:
php artisan vendor:publish --provider="dimonka2\flatstate\FlatstateServiceProvider"
- Run the migrations:
php artisan migrate
To add states to a model, use the Stateable
trait. Define the states in the model's $states
property:
use dimonka2\flatstate\Traits\Stateable;
class Project extends Model
{
use Stateable;
protected $states = [
'state_id' => [
'type' => 'projects',
'default' => 'pr_active',
['name' => 'Active', 'key' => 'pr_active', 'descriptions' => '..', 'icon' => 'fa fa-info', 'color' => 'danger'],
],
];
}
You can access and format states using the methods provided by the Stateable
trait:
$project = Project::find(1);
// Get the state key
$stateKey = $project->getState('state_id');
// Format the state with an icon
$formattedState = $project->formatStateState(true);
You can use the state
directive in Blade templates to format states:
@state($project->state_id)
The State Service Manager class provides various methods to interact with the states. It is available via the Flatstate
facade.
use dimonka2\flatstate\Flatstate;
// Get state by key
$state = Flatstate::getState('pr_active');
// Get several states by key as array
$state = Flatstate::getState(['pr_active', 'pr_suspended']);
// Get state key by ID
$stateKey = Flatstate::getStateKey(1);
// Get state icon by ID
$stateIcon = Flatstate::getStateIcon(1);
// Get a list of states by type
$stateList = Flatstate::getStateList('projects');
You can clear the state cache using the clearCache
method:
Flatstate::clearCache();
You can format states using the formatState
method:
$formattedState = Flatstate::formatState($state, true);
The package provides several Artisan commands:
php artisan flatstate:list
- List all defined statesphp artisan flatstate:seed
- Seed the states into the databasephp artisan flatstate:generate
- Generate TypeScript definitions for the states
The flatstate:list
command allows you to list available state categories or the states within a specific category.
- To list all state categories and the count of states in each category, run:
php artisan flatstate:list
- To list all states within a specific category, specify the category as an argument:
php artisan flatstate:list {category}
Example usage:
php artisan flatstate:list projects
This will output a table with the state details such as state_type
, state_key
, name
, and other fillable fields.
The flatstate:seed
command allows you to seed the states defined in your models into the database.
php artisan flatstate:seed
This command processes the states defined in your models and saves them to the database. It looks for models that use the Stateable
trait and reads their $states
property to determine which states to seed.
You can also specify a single model class to seed:
php artisan flatstate:seed {class?}
The flatstate:generate
command generates a TypeScript list of all states or states within a specific category. This is useful for maintaining type safety and consistency in your front-end code.
php artisan flatstate:generate {category?}
To generate TypeScript definitions for all states:
php artisan flatstate:generate
To generate TypeScript definitions for a specific category, pass the category as an argument:
php artisan flatstate:generate projects
This will output a TypeScript file with all the state definitions, which you can then use in your front-end code.
The configuration file flatstate.php
will be published to your application's config directory. You can customize the settings as needed.
This package is open-sourced software licensed under the MIT license.