PHP code example of zvermafia / larastate

1. Go to this page and download the library: Download zvermafia/larastate library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

zvermafia / larastate example snippets


        /**
         * Get the validation rules that apply to the request.
         *
         * @return array
         */
        public function rules()
        {
            return [
                // ... other validation rules
                'role' => [
                    '

        <li>{{ $user->name }}</li>
        <li>{{ $user->email }}</li>
        <li>{{ trans("user.states.role.{$user->role}") }}</li>
    

        @foreach (['member', 'moderator', 'administrator'] as $value)
            <option value="{{ $value }}">@lang("user.states.role.{$value}")</option>
        @endforeach
    

 // app/States/UserState.php

namespace App\States\UserState;

use Zvermafia\Larastate\Abstracts\StateAbstract;

class UserState extends StateAbstract
{
    /** @var array */
    public const ROLE = [
        'member' => 'member',
        'moderator' => 'moderator',
        'administrator' => 'administrator',
    ];
}

        /**
         * Get the validation rules that apply to the request.
         *
         * @param \App\States\UserState $user_states
         * @return array
         */
        public function rules(UserState $user_states)
        {
            return [
                // ... other validation rules
                'role' => [
                    '

        <li>{{ $user->name }}</li>
        <li>{{ $user->email }}</li>
        <li>{{ $user_states->getRoleLocale($user->role) }}</li>
    

        @foreach ($user_states->getRoleValuesWithLocales() as $value => $locale)
            <option value="{{ $value }}">{{ $locale }}</option>
        @endforeach
    

    
    
    namespace App\States;
    
    use Zvermafia\Larastate\Abstracts\StateAbstract;

    class PostState extends StateAbstract
    {
        /** @var array */
        public const STATUS = [
            'draft' => 0,
            'published' => 1,
        ];
        
        /** @var array */
        public const TYPE = [
            'info' => 0,
            'blog' => 1,
            'news' => 2,
        ];
    }
    

     // resources/lang/en/entities/post.php

    return [
        // here's other localization like attribute/property...
        
        'state' => [
            'status' => [
                App\States\PostState::STATUS['draft'] => 'Draft',
                App\States\PostState::STATUS['published'] => 'Published',
            ],
            'type' => [
                App\States\PostState::TYPE['info'] => 'Static page',
                App\States\PostState::TYPE['blog'] => 'Blog post',
                App\States\PostState::TYPE['news'] => 'News post',
            ],
        ],
    ];
    
 php
$post_states = new App\States\PostState();

$post_states->getTypeValues(); // [0, 1, 2]
$post_states->getTypeValuesWithLocales(); // [0 => 'Static page', 1 => 'Blog post', 2 => 'News post']
$post_states->getTypeValues(); // 'Blog post'