LoginSignup
1
1

More than 3 years have passed since last update.

Laravel-Enum 導入2

Posted at

はじめに

前提

Enum ファイルの中身

app/Enum/TestText.php
<?php

namespace App\Enums;

final class TestText extends Enum
{
    const TEST = "test";
    const TEXT = "text";
}

日本語ファイルの中身

app/resources/lang/ja/enums.php
<?php
// php artisan で作成したクラス
use App\Enums\TestText;

return [

    TestText::class=> [
        TestText::TEST => 'テスト',
        TestText::TEXT => 'テキスト',
    ],
];

使ってみる

インスタンスを生成して key/ value を取得

// 全体の インスタンスを取得
$testText = TestText::getInstances();

// 以下のように値が取得できる
$testText[TestText::TEST]->key;   // TEST 
$testText[TestText::TEST]->value; // test
$testText[TestText::TEST]->description; // テスト
// 指定したキーのインスタンスを取得する
TestText::TEST()->key;    // TEST
TestText::TEST()->value;  // test
TestText::TEST()->description // テスト

key の一覧を取得する

TestText::getKeys(); // return['TEST', 'TEXT'];

value の一覧を取得する

TestText::getValues(); // return['test', 'text'];
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1