PHPackages                             zvermafia/larastate - 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. [Localization &amp; i18n](/categories/localization)
4. /
5. zvermafia/larastate

ActiveLibrary[Localization &amp; i18n](/categories/localization)

zvermafia/larastate
===================

A convenient way to access entity states' values and their localizations.

v0.1.3(5y ago)011MITPHPPHP ~7.2

Since Mar 1Pushed 5y ago1 watchersCompare

[ Source](https://github.com/zvermafia/larastate)[ Packagist](https://packagist.org/packages/zvermafia/larastate)[ Docs](https://github.com/zvermafia/larastate)[ RSS](/packages/zvermafia-larastate/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (3)Versions (5)Used By (0)

Larastate — a main source for your entity states values
=======================================================

[](#larastate--a-main-source-for-your-entity-states-values)

[![Latest Version on Packagist](https://camo.githubusercontent.com/88590c4898af3fe50f2ceb69351c75203ad79db70068bec01ace8b1befa4890e/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f7a7665726d616669612f6c61726173746174652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/zvermafia/larastate)[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE.md)[![Build Status](https://camo.githubusercontent.com/5949ae969998fe8ea7d1ea8a38f6f52a3f9617dd6234c3aa8510683a968cceee/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f7a7665726d616669612f6c61726173746174652f6d61737465722e7376673f7374796c653d666c61742d737175617265)](https://travis-ci.org/zvermafia/larastate)[![Coverage Status](https://camo.githubusercontent.com/c1ef92a5727a980f0ae293f7328c0366689433b796c6a90fd648991d696545f5/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f636f7665726167652f672f7a7665726d616669612f6c61726173746174652e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/zvermafia/larastate/code-structure)[![Quality Score](https://camo.githubusercontent.com/2cf6425a7d0ba3929dc9ecc4a429cd57410cfd761a7a11df9ed14f4dc1947213/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f7a7665726d616669612f6c61726173746174652e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/zvermafia/larastate)[![Total Downloads](https://camo.githubusercontent.com/aebf14a53cb4b055978168c6f73270af1cdadbd0b2e828b730a3a0d047304254/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f7a7665726d616669612f6c61726173746174652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/zvermafia/larastate)

Easy access to entity states values and to their localizations.

### Navigation by sections

[](#navigation-by-sections)

- [Introduction](#introduction)
    - [The problem](#the-problem)
    - [A solution](#a-solution)
- [Install](#install)
- [Setup](#setup)
- [Usage](#usage)
- [Alternatives](#alternatives)
- [Change log](#change-log)
- [Contributing](#contributing)
- [Security](#security)
- [Credits](#credits)
- [License](#license)

Introduction
------------

[](#introduction)

### The problem

[](#the-problem)

When you have an entity with some states you usually need to access to those states' values and to their localizations. Let's consider the following cases:

*Let's assume we have a User entity with a role state. And the role state can only accept 'member', 'moderator' and 'administrator' values.*

- **When you need to validate a state's acceptable values.** So when we try to create a new user we need to validate the role state value. But how to do it if we don't have a main source for that values? And usually we may do like below:

    ```
        /**
         * Get the validation rules that apply to the request.
         *
         * @return array
         */
        public function rules()
        {
            return [
                // ... other validation rules
                'role' => [
                    'required',
                    Rule::in(['member', 'moderator', 'administrator']), // 🙁 values are hardcoded
                ],
            ];
        }
    ```
- **When you need to display localization for one of the state's values.** For example when you need to give an information about a user's role:

    ```
        {{ $user->name }}
        {{ $user->email }}
        {{ trans("user.states.role.{$user->role}") }}
    ```

    🙁 Localization file path, file name and locale key are hardcoded.
- **When you need to display those values with locales.** For example in a select box:

    ```
        @foreach (['member', 'moderator', 'administrator'] as $value)
            @lang("user.states.role.{$value}")
        @endforeach
    ```

    🙁 Hardcoded, hardcoded, ...

And yes when you hardcoded those values in different places of your project then it will be harder to change (add, remove, rename) those values. Because you don't use a main source for those values.

### A solution

[](#a-solution)

Unfortunately Laravel doesn't provide a solution for this problem out of the box. So I've created this package. With this package you will not have problems in the above section! Let's solve those problems in the appropriate order.

Firstly create a `States` folder in the `app` directory, then create a `UserState` class in it with extending a `StateAbstract` abstract class. And define a `ROLE` constant in the class with those values from the above section. You should have something below:

```
