概要?
Laravel-Enum の使い方をまとめておこうと思い。記事を書く所存。
準備
laraevl のバージョン
$ php artisan --version
Laravel Framework 5.8.16
使えるようにする
以下のコマンドを実行すると、Laravel 上で Enum(列挙型)が使用できるようになる。
Enumって、ラジオボタンとかセレクトボックスの中身とかに使えて便利だと思う。
$ composer require bensampo/laravel-enum
使いたい
コマンドからクラスを作成する
コマンドを実行して、Enum クラスを作成する
$ php artisan make:enum TestText
Enum created successfully.
正常に作成すると、app/Enums
のしたに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 => 'テキスト',
],
];
ただ上記のファイルの恩恵を受けるためには、app/config/app.php
のlocale
を日本語に設定しておく必要がある
<?php
return [
// 略
'locale' => 'ja',
// 略
];
とりあえず View で使ってみる。
例えば、ラジオボタンとか。
toSelectArray
が key=>value
で返してくれるので便利。
これはlalavel-enum
のメソッド。便利。
<div class="input-group col-12 col-md-6">
{{ Form::label("test_list_label", "テストラジオ", ['class'=> 'required']) }}
<div class="radios">
@foreach (\App\Enums\TestText::toSelectArray() as $value => $item)
<div class="radio">
{{ Form::radio('test_list_item', $value, $value === 'test', [
'id' => "test_list_item--{$value}",
]) }}
{{ Form::label("test_list_item--{$value}", $item, []) }}
</div>
@endforeach
</div>
</div>
Mysql のデータベースで 上記Enum 使いたい
Mysql のバージョン
mysql> select version();
+-----------+
| version() |
+-----------+
| 5.7.28 |
+-----------+
1 row in set (0.00 sec)
Migration ファイルを作成する
- 今回は、testsテーブルに Enum の check_type カラムを追加する
$ php artisan make:migration add_column_check_type --table=tests
> Created Migration: YYYY_MM_DD_HHiiss_add_column_check_type
Migration ファイルを編集する
- 作成したMigration ファイルを編集し、Enum 型を追加する
YYYY_MM_DD_HHiiss_add_column_check_type.php
use App\Enums\TestText; // 追加したい Enum
class AddColumnCheckType extends Migration
{
public function up()
{
Schema::table('tests', function (Blueprint $table) {
// enum の check_type を 初期値 TEST(test) でid の下に追加
$table->enum('check_type', TestText::getValues())
->default(TestText::TEST)
->after('id');
});
}
public function down()
{
Schema::table('tests', function (Blueprint $table) {
// check_type カラムを削除
$table->dropColumn('check_type');
});
}
}
Migration コマンドを実行する
- 作成後、Migration コマンドを実行する
$ php artisan migrate
> Migrating: YYYY_MM_DD_HHiiss_add_column_check_type
> Migrated: YYYY_MM_DD_HHiiss_add_column_check_type
- 正常に実行できたら、MYSQL で確認!
mysql> show columns from tests where Field='check_type';
+------------+---------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+---------------------+------+-----+---------+-------+
| check_type | enum('test','text') | NO | | test | |
+------------+---------------------+------+-----+---------+-------+
1 row in set (0.00 sec)
できてる。やったー。
Enum カラムを Model にキャストしておく
Models/Test.php
class Test extends Model
{
// プロパティのキャスト
protected $enumCasts = [
'check_type' => \App\Enums\TestText::class,
];
}
- キャストをしておくことによって、呼び出しが簡単になる
例えば、
$tests->check_type->test
とかで Enum の内容を呼び出せる