PHPackages                             dimonka2/flatstate - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. dimonka2/flatstate

ActiveLibrary[Database &amp; ORM](/categories/database)

dimonka2/flatstate
==================

Model states manager with simplified state declaration for Laravel

193PHPCI failing

Since Jun 18Pushed 1y ago1 watchersCompare

[ Source](https://github.com/Dimonka2/flatstate)[ Packagist](https://packagist.org/packages/dimonka2/flatstate)[ RSS](/packages/dimonka2-flatstate/feed)WikiDiscussions master Synced 5d ago

READMEChangelog (1)DependenciesVersions (1)Used By (0)

Laravel Flatstate
=================

[](#laravel-flatstate)

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.

Features
--------

[](#features)

- 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

Installation
------------

[](#installation)

1. Install the package via Composer:

```
composer require dimonka2/flatstate
```

2. Publish the configuration and migration files:

```
php artisan vendor:publish --provider="dimonka2\flatstate\FlatstateServiceProvider"
```

3. Run the migrations:

```
php artisan migrate
```

Usage
-----

[](#usage)

### Adding States to a Model

[](#adding-states-to-a-model)

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'],
        ],
    ];
}
```

### Accessing and Formatting States

[](#accessing-and-formatting-states)

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);
```

### Blade Integration

[](#blade-integration)

You can use the `state` directive in Blade templates to format states:

```
@state($project->state_id)
```

### State Service Manager

[](#state-service-manager)

The State Service Manager class provides various methods to interact with the states. It is available via the `Flatstate` facade.

#### Retrieving States

[](#retrieving-states)

```
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');
```

#### Managing Cache

[](#managing-cache)

You can clear the state cache using the `clearCache` method:

```
Flatstate::clearCache();
```

#### Formatting States

[](#formatting-states)

You can format states using the `formatState` method:

```
$formattedState = Flatstate::formatState($state, true);
```

Commands
--------

[](#commands)

The package provides several Artisan commands:

- `php artisan flatstate:list` - List all defined states
- `php artisan flatstate:seed` - Seed the states into the database
- `php artisan flatstate:generate` - Generate TypeScript definitions for the states

#### List Available States

[](#list-available-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.

#### Seeding States

[](#seeding-states)

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?}
```

### Generate TypeScript Definitions

[](#generate-typescript-definitions)

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.

#### Usage

[](#usage-1)

```
php artisan flatstate:generate {category?}
```

#### Example

[](#example)

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.

Configuration
-------------

[](#configuration)

The configuration file `flatstate.php` will be published to your application's config directory. You can customize the settings as needed.

License
-------

[](#license)

This package is open-sourced software licensed under the [MIT license](https://opensource.org/licenses/MIT).

###  Health Score

17

—

LowBetter than 6% of packages

Maintenance27

Infrequent updates — may be unmaintained

Popularity11

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity18

Early-stage or recently created project

 Bus Factor1

Top contributor holds 84.2% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

### Community

Maintainers

![](https://www.gravatar.com/avatar/77a8670827b6d4a1bff647d3b968379b8da50a4c709c37c3a22e343bfe38ab27?d=identicon)[dimonka2](/maintainers/dimonka2)

---

Top Contributors

[![Dimonka2](https://avatars.githubusercontent.com/u/19688204?v=4)](https://github.com/Dimonka2 "Dimonka2 (32 commits)")[![Dimonka01](https://avatars.githubusercontent.com/u/25383254?v=4)](https://github.com/Dimonka01 "Dimonka01 (6 commits)")

### Embed Badge

![Health badge](/badges/dimonka2-flatstate/health.svg)

```
[![Health](https://phpackages.com/badges/dimonka2-flatstate/health.svg)](https://phpackages.com/packages/dimonka2-flatstate)
```

###  Alternatives

[doctrine/orm

Object-Relational-Mapper for PHP

10.2k285.3M6.2k](/packages/doctrine-orm)[jdorn/sql-formatter

a PHP SQL highlighting library

3.9k115.1M102](/packages/jdorn-sql-formatter)[illuminate/database

The Illuminate Database package.

2.8k52.4M9.4k](/packages/illuminate-database)[mongodb/mongodb

MongoDB driver library

1.6k64.0M546](/packages/mongodb-mongodb)[ramsey/uuid-doctrine

Use ramsey/uuid as a Doctrine field type.

90340.3M211](/packages/ramsey-uuid-doctrine)[reliese/laravel

Reliese Components for Laravel Framework code generation.

1.7k3.4M16](/packages/reliese-laravel)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
