{"id":9829,"date":"2021-11-22T07:00:15","date_gmt":"2021-11-21T22:00:15","guid":{"rendered":"https:\/\/blog.capilano-fw.com\/?p=9829"},"modified":"2021-11-16T18:29:34","modified_gmt":"2021-11-16T09:29:34","slug":"%e3%80%90laravel%e3%80%91php-8-1-%e3%81%8b%e3%82%89%e4%bd%bf%e3%81%88%e3%82%8b-enum%e3%81%a7%e3%80%81%e9%81%b8%e6%8a%9e%e8%82%a2%e3%80%9c%e3%83%90%e3%83%aa%e3%83%87%e3%83%bc%e3%82%b7%e3%83%a7%e3%83%b3","status":"publish","type":"post","link":"https:\/\/blog.capilano-fw.com\/?p=9829","title":{"rendered":"\u3010Laravel\u3011PHP 8.1 \u304b\u3089\u4f7f\u3048\u308b Enum\u3067\u3001\u9078\u629e\u80a2\u301c\u30d0\u30ea\u30c7\u30fc\u30b7\u30e7\u30f3\u307e\u3067\u5b9f\u88c5\u3057\u3066\u307f\u308b"},"content":{"rendered":"<p>\u3055\u3066\u3055\u3066\u3001\u300c<strong>\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u8a00\u8a9e<\/strong>\u300d\u3068\u4e00\u53e3\u306b\u8a00\u3063\u3066\u3082\u4e16\u306e\u4e2d\u306b\u306f<code>PHP<\/code>\u4ee5\u5916\u306b\u3082<code>Pyhon<\/code>\u3084<code>Java<\/code>\u306a\u3069\u305f\u304f\u3055\u3093\u306e\u3082\u306e\u304c\u5b58\u5728\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n<p>\u305d\u3057\u3066\u3001\u305d\u3093\u306a\u4ed6\u306e\u8a00\u8a9e\u3092\u4f7f\u3063\u305f\u3053\u3068\u304c\u3042\u308b\u4eba\u306a\u3089\u3001\uff08\u304d\u3063\u3068\uff09\u601d\u3046\u3053\u3068\u3002<\/p>\n<p>\u305d\u308c\u306f\u30fb\u30fb\u30fb\u30fb\u30fb\u30fb<\/p>\n<p><strong style=\"font-size: 35px;\">\u3042\u3063\u3061\u306e\u8a00\u8a9e\u3060\u3063\u305f\u3089\u3067\u304d\u305f\u306e\u306b\u306a\u2026\ud83d\ude05<\/strong><\/p>\n<p>\u3067\u3059\u3002<\/p>\n<p>\u7d50\u69cb\u524d\u3067\u3059\u304c\u3001\u79c1\u306f<code>Android<\/code>\u30a2\u30d7\u30ea\u3092\u958b\u767a\u3057\u3066\u3044\u305f\u306e\u3067\u3059\u304c\u3001\u305d\u306e\u3068\u304d\u300c<strong>\u3053\u308c\u4fbf\u5229\u3060\u306a<\/strong>\u300d\u3068\u611f\u3058\u3066\u3044\u305f\u306e\u304c\u4eca\u56de\u30c6\u30fc\u30de\u306e<\/p>\n<p><strong style=\"font-size: 35px;\">Enum\uff08\u3044\u30fc\u306a\u3080\uff09<\/strong><\/p>\n<p>\u3067\u3059\u3002<\/p>\n<p><code>Enum<\/code>\u3068\u306f\u3001\u7c21\u5358\u306b\u3044\u3046\u3068\u300c<strong>\u5148\u306b<\/strong><strong>\u30b0\u30eb\u30fc\u30d7\u306e\u30e1\u30f3\u30d0\u30fc\u3092\u6c7a\u3081\u3066\u304a\u304f<\/strong>\u300d\u4ed5\u7d44\u307f\u3067\u3059\u3002<\/p>\n<p>\u4f8b\u3048\u3070\u3001\u5341\u4e8c\u652f\u3067\u898b\u3066\u307f\u307e\u3057\u3087\u3046\u3002<\/p>\n<ul>\n<li>\u5b50<\/li>\n<li>\u4e11<\/li>\n<li>\u5bc5<\/li>\n<li>\u536f<\/li>\n<li>\u8fb0<\/li>\n<li>\u5df3<\/li>\n<li>\u5348<\/li>\n<li>\u672a<\/li>\n<li>\u7533<\/li>\n<li>\u9149<\/li>\n<li>\u620c<\/li>\n<li>\u4ea5<\/li>\n<\/ul>\n<p>\u3082\u3061\u308d\u3093\u3067\u3059\u304c\u3001\u3053\u306e\u4e2d\u306b\u306f\u300c<strong>\u30d1\u30f3\u30c0<\/strong>\u300d\u3084\u300c<strong>\u30ab\u30e2\u30ce\u30cf\u30b7<\/strong>\u300d\u306f\u5165\u308b\u3053\u3068\u306f\u3067\u304d\u307e\u305b\u3093\u3002<strong>\u3082\u3046\u30b0\u30eb\u30fc\u30d7\u30fb\u30e1\u30f3\u30d0\u30fc\u304c\u6c7a\u5b9a\u3057\u3066\u3044\u308b\u304b\u3089<\/strong>\u3067\u3059\u3002<\/p>\n<p>\u3067\u306f\u3001\u306a\u305c\u30b0\u30eb\u30fc\u30d7\u5316\u3059\u308b\u306e\u304b\u3068\u3044\u3046\u3068\u300c<strong>\u4e88\u671f\u305b\u306c\u5024\u3092\u6271\u308f\u306a\u3044\u3088\u3046\u306b\u3067\u304d\u308b<\/strong>\u300d\u3068\u3044\u3046\u30e1\u30ea\u30c3\u30c8\u304c\u3042\u308b\u304b\u3089\u3067\u3059\u306d\u3002<\/p>\n<p>\u305d\u3057\u3066\u3001\u9577\u3089\u304f<code>PHP<\/code>\u306b\u306f\u3053\u306e<code>Enum<\/code>\u306e\u6a5f\u80fd\u304c\u3064\u3044\u3066\u3044\u306a\u304b\u3063\u305f\u306e\u3067\u3059\u304c\u3001\u5b9f\u306f\u6b21\u671f\u30d0\u30fc\u30b8\u30e7\u30f3<code>8.1<\/code>\uff082021.11.25 \u30ea\u30ea\u30fc\u30b9\u4e88\u5b9a\uff09\u304b\u3089\u4f7f\u3048\u308b\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<p>\u305d\u3053\u3067\u2757<\/p>\n<p>\u4eca\u56de\u306f\u3001<code>Laravel + Enum<\/code>\u3067\u30bb\u30ec\u30af\u30c8\u30dc\u30c3\u30af\u30b9\u3092\u3064\u304f\u308a\u3001\u3055\u3089\u306b\u3001\u305d\u308c\u3092\u9001\u4fe1\u3057\u305f\u3068\u304d\u306e\u30d0\u30ea\u30c7\u30fc\u30b7\u30e7\u30f3\u307e\u3067\u5b9f\u88c5\u3057\u3066\u307f\u308b\u3053\u3068\u306b\u3057\u307e\u3057\u305f\u3002<\/p>\n<p>\u305c\u3072\u4f55\u304b\u306e\u53c2\u8003\u306b\u306a\u308a\u307e\u3057\u305f\u3089\u5b09\u3057\u3044\u3067\u3059\u3002<\/p>\n<p>\u203b \u306a\u304a\u3001<code>PHP 8.1<\/code>\u306e\u30ea\u30ea\u30fc\u30b9\u306e\u95a2\u4fc2\u3067\u8a18\u4e8b\u516c\u958b\u306e\u9806\u756a\u3092\u5909\u66f4\u3055\u305b\u3066\u3044\u305f\u3060\u304d\u307e\u3057\u305f\u3002\u300c<strong>Laravel + Leaflet \u5730\u56f3\u3067\u99c5\u540d\u691c\u7d22\u3067\u304d\u308b\u3088\u3046\u306b\u3059\u308b<\/strong>\u300d\u306f\u6765\u9031\u516c\u958b\u3055\u305b\u3066\u3044\u305f\u3060\u304d\u307e\u3059\u3002\u3082\u3057\u697d\u3057\u307f\u306b\u3057\u3066\u3044\u305f\u3060\u3044\u3066\u3044\u305f\u3089\u30b4\u30e1\u30f3\u30ca\u30b5\u30a4\u3002m(_ _)m<\/p>\n<p style=\"text-align: center;\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-3566 aligncenter\" src=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2019\/04\/no_1.png\" alt=\"\" width=\"306\" height=\"320\" srcset=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2019\/04\/no_1.png 306w, https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2019\/04\/no_1-287x300.png 287w\" sizes=\"auto, (max-width: 306px) 100vw, 306px\" \/>\u300c\u3082\u3046\u3059\u30508.1\u30ea\u30ea\u30fc\u30b9\u3067\u3059\u3088\ud83d\udc4d\u300d<\/p>\n<p><strong>\u958b\u767a\u74b0\u5883\uff1a<\/strong> PHP 8.1.0\uff08RC6\uff09\u3001Laravel 8.70<\/p>\n<h1>\u9078\u629e\u80a2\u306b\u3064\u3044\u3066<\/h1>\n<p>\u4eca\u56de<code>Enum<\/code>\u3067\u3064\u304f\u308b\u9078\u629e\u80a2\u306f\u300c<strong>\u66f8\u7c4d\u306e\u30ab\u30c6\u30b4\u30ea<\/strong>\u300d\u3067\u3001\u4ee5\u4e0b\u306e\u3068\u304a\u308a\u3067\u3059\u3002<\/p>\n<ul>\n<li>\u672c<\/li>\n<li>\u96d1\u8a8c<\/li>\n<li>\u65b0\u805e<\/li>\n<\/ul>\n<p>\u3067\u306f\u5b9f\u969b\u306b\u4f5c\u696d\u3092\u9032\u3081\u3066\u3044\u304d\u307e\u3057\u3087\u3046\u2757<\/p>\n<h1>Enum\u3092\u3064\u304f\u308b<\/h1>\n<p>\u3067\u306f\u3001\u65e9\u901f<code>Enum<\/code>\u3092\u3064\u304f\u308a\u307e\u3057\u3087\u3046\u3002<\/p>\n<p>\u3068\u306f\u3044\u3063\u3066\u3082\u3001<code>Enum<\/code>\u306f\u3088\u304f\u958b\u767a\u3067\u3082\u3064\u304b\u3046<strong>\u300c\u30af\u30e9\u30b9\u300d\u3092\u3082\u3063\u3068\u30b7\u30f3\u30d7\u30eb\u306b\u3057\u305f\u3082\u306e<\/strong>\u3068\u8003\u3048\u3066\u554f\u984c\u306a\u3044\u306e\u3067\u3001\u305d\u3093\u306a\u306b\u8907\u96d1\u3067\u306f\u3042\u308a\u307e\u305b\u3093\u3002<\/p>\n<p>\u5b9f\u969b\u306e\u30b3\u30fc\u30c9\u306f\u3053\u3046\u306a\u308a\u307e\u3059\u3002<\/p>\n<p><strong>app\/Enums\/Category.php<\/strong><\/p>\n<pre><strong>&lt;?php<\/strong>\r\n\r\n<strong>namespace App\\Enums;<\/strong>\r\n\r\n<strong>enum Category: int<\/strong>\r\n<strong>{<\/strong>\r\n<strong>    \/\/ \u57fa\u672c\u60c5\u5831<\/strong>\r\n<strong>    case Book = 1;<\/strong>\r\n<strong>    case Magazine = 2;<\/strong>\r\n<strong>    case Newspaper = 3;<\/strong>\r\n\r\n<strong>    \/\/ \u65e5\u672c\u8a9e\u3092\u8ffd\u52a0<\/strong>\r\n<strong>    public function label(): string<\/strong>\r\n<strong>    {<\/strong>\r\n<strong>        return match($this)<\/strong>\r\n<strong>        {<\/strong>\r\n<strong>            Category::Book =&gt; '\u672c',<\/strong>\r\n<strong>            Category::Magazine =&gt; '\u96d1\u8a8c',<\/strong>\r\n<strong>            Category::Newspaper =&gt; '\u65b0\u805e',<\/strong>\r\n<strong>        };<\/strong>\r\n<strong>    }<\/strong>\r\n<strong>}<\/strong><\/pre>\n<p>\u307e\u305a\u3001<code>Enum<\/code>\u3067\u5fc5\u305a\u5fc5\u8981\u306b\u306a\u3063\u3066\u304f\u308b\u306e\u304c\u300c\u540d\u524d\u300d\u3067\u3001\u4eca\u56de\u3067\u306f\u4ee5\u4e0b\u306e\uff13\u3064\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<ul>\n<li>Book<\/li>\n<li>Magazine<\/li>\n<li>Newspaper<\/li>\n<\/ul>\n<p>\u3053\u308c\u3060\u3051\u3067\u3082<code>Enum<\/code>\u3068\u3057\u3066\u306f\u4f7f\u3048\u308b\u306e\u3067\u3059\u304c\u3001<code>category_id<\/code>\u3068\u3057\u3066\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u306b\u4fdd\u5b58\u3059\u308b\u3053\u3068\u3092\u60f3\u5b9a\u3057\u3066\u3044\u308b\u306e\u3067\u3001\u4eca\u56de\u306f\u305d\u308c\u305e\u308c<code>1<\/code>\u301c<code>3<\/code>\u306eID\u3092\u8ffd\u52a0\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n<p>\u306a\u304a\u3001<\/p>\n<pre>enum Category: <strong>int\r\n<\/strong><\/pre>\n<p>\u3068\u66f8\u3044\u3066\u3044\u308b\u306e\u306f\u3001\u3053\u306e<code>ID<\/code>\u304c\u6570\u5024\u3060\u304b\u3089\u3067\u3059\u3002\u3082\u3057\u6587\u5b57\u5217\u3092\u4f7f\u3044\u305f\u3044\u5834\u5408\u306f<strong>string<\/strong>\u306b\u306a\u308a\u307e\u3059\u306e\u3067\u3001\u6c17\u3092\u3064\u3051\u3066\u304f\u3060\u3055\u3044\u3002\uff08<code>enum<\/code>\u3067\u306f\u3001<code>TypeScript<\/code>\u307f\u305f\u3044\u306b\u578b\u304c\u91cd\u8981\u306b\u306a\u3063\u3066\u304d\u307e\u3059\uff09<\/p>\n<p>\u305d\u3057\u3066\u3001\u65e5\u672c\u8a9e\u74b0\u5883\u3067\u306f\u30bb\u30ec\u30af\u30c8\u30dc\u30c3\u30af\u30b9\u306e\u9078\u629e\u80a2\u306b\u300c<strong>Book<\/strong>\u300d\u3084\u300c<strong>Magazine<\/strong>\u300d\u3068\u8868\u793a\u3055\u308c\u308b\u306e\u306f\u3042\u307e\u308a\u4f7f\u3044\u52dd\u624b\u304c\u3044\u3044\u3068\u306f\u3044\u3048\u307e\u305b\u3093\u3002<\/p>\n<p>\u305d\u306e\u305f\u3081\u3001\u305d\u308c\u305e\u308c\u65e5\u672c\u8a9e\u540d\u3092\u8ffd\u52a0\u3057\u307e\u3059\u3002<\/p>\n<p>\u305d\u308c\u304c\u3001<code>label()<\/code>\u306e\u90e8\u5206\u3067\u3059\u3002\uff08\u306a\u304a\u3001<code>label<\/code>\u3068\u3044\u3046\u540d\u524d\u306f\u597d\u304d\u306b\u5909\u66f4\u3057\u3066OK\u3067\u3059\u3088\ud83d\udc4d\uff09<\/p>\n<p>\u305d\u3057\u3066\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u3059\u308b\u3068\u65e5\u672c\u8a9e\u540d\u3092\u53d6\u5f97\u3067\u304d\u308b\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<pre><strong>$label = Category::Book-&gt;label();<\/strong> \/\/ \u672c<\/pre>\n<h1>\u30b3\u30f3\u30c8\u30ed\u30fc\u30e9\u30fc\u3092\u3064\u304f\u308b<\/h1>\n<p>\u3067\u306f\u3001<code>Enum<\/code>\u306e\u30c6\u30b9\u30c8\u7528\u306b\u30b3\u30f3\u30c8\u30ed\u30fc\u30e9\u30fc\u3092\u3064\u304f\u308a\u307e\u3059\u3002<br \/>\n\u4ee5\u4e0b\u306e\u30b3\u30de\u30f3\u30c9\u3092\u5b9f\u884c\u3057\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n<pre><strong>php artisan make:controller EnumController<\/strong><\/pre>\n<p>\u3059\u308b\u3068\u3001\u30d5\u30a1\u30a4\u30eb\u304c\u4f5c\u6210\u3055\u308c\u308b\u306e\u3067\u4e2d\u8eab\u3092\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u3057\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n<p><strong>app\/Http\/Controllers\/EnumController.php<\/strong><\/p>\n<pre>&lt;?php\r\n\r\nnamespace App\\Http\\Controllers;\r\n\r\n<strong>use App\\Enums\\Category;<\/strong>\r\nuse Illuminate\\Http\\Request;\r\nuse Illuminate\\Support\\Arr;\r\nuse Illuminate\\Validation\\Rule;\r\n\r\nclass EnumController extends Controller\r\n{\r\n    <strong>public function create()<\/strong>\r\n<strong>    {<\/strong>\r\n<strong>        $categories = Category::cases();<\/strong>\r\n\r\n<strong>        return view('enum.create', compact('categories'));<\/strong>\r\n<strong>    }<\/strong>\r\n\r\n<strong>    public function store(Request $request)<\/strong>\r\n<strong>    {<\/strong>\r\n<strong>        $categories = Category::cases();<\/strong>\r\n<strong>        $category_ids = Arr::pluck($categories, 'value');<\/strong>\r\n\r\n<strong>        $request-&gt;validate([<\/strong>\r\n<strong>            'category_id' =&gt; ['required', Rule::in($category_ids)]<\/strong>\r\n<strong>        ]);<\/strong>\r\n\r\n<strong>        return '\u30d0\u30ea\u30c7\u30fc\u30b7\u30e7\u30f3\u901a\u904e\u3057\u307e\u3057\u305f\uff01';<\/strong>\r\n<strong>    }<\/strong>\r\n}<\/pre>\n<p>\u307e\u305a\u3001<code>create()<\/code>\u3067\u306f\u3001\u5148\u307b\u3069\u3064\u304f\u3063\u305f<code>enum<\/code>\u3092\u30d3\u30e5\u30fc\u306b\u6e21\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n<p>\u307e\u305f\u3001<code>store()<\/code>\u3067\u306f\u9078\u629e\u3055\u308c\u305f<code>ID<\/code>\u304c\u300c<strong>\u4e8b\u524d\u306b\u30bb\u30c3\u30c8\u3057\u305f\uff13\u3064\u306e\u4e2d\u306b\u5165\u3063\u3066\u3044\u308b\u304b\uff1f<\/strong>\u300d\u3092\u30c1\u30a7\u30c3\u30af\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n<h1>\u30d3\u30e5\u30fc\u3092\u3064\u304f\u308b<\/h1>\n<p>\u3067\u306f\u3001\u5148\u307b\u3069\u6307\u5b9a\u3057\u305f\u30d3\u30e5\u30fc\u3092\u3064\u304f\u3063\u3066\u3044\u304d\u307e\u3057\u3087\u3046\u3002<\/p>\n<p><strong>resources\/views\/enum\/create.blade.php<\/strong><\/p>\n<pre>&lt;html&gt;\r\n&lt;head&gt;\r\n    &lt;link href=\"https:\/\/cdn.jsdelivr.net\/npm\/bootstrap@5.0.1\/dist\/css\/bootstrap.min.css\" rel=\"stylesheet\"&gt;\r\n&lt;\/head&gt;\r\n&lt;body&gt;\r\n&lt;div id=\"app\" class=\"p-5\"&gt;\r\n\r\n    &lt;h1 class=\"mb-5\"&gt;Enum\uff08PHP 8.1 \u4ee5\u4e0a\uff09 \u3092\u4f7f\u3063\u3066&lt;br&gt;\u9078\u629e\u80a2\u3092\u3064\u304f\u308b\u30b5\u30f3\u30d7\u30eb&lt;\/h1&gt;\r\n\r\n    &lt;div class=\"row\"&gt;\r\n        &lt;div class=\"col-4\"&gt;\r\n\r\n            &lt;form action=\"\/enum\" method=\"POST\"&gt;\r\n                @csrf\r\n\r\n                <strong>&lt;select name=\"category_id\" class=\"form-control\"&gt;<\/strong>\r\n<strong>                    &lt;option value=\"\"&gt;\u25bc \u4ee5\u4e0b\u304b\u3089\u9078\u629e&lt;\/option&gt;<\/strong>\r\n<strong>                    @foreach($categories as $category)<\/strong>\r\n<strong>                        &lt;option value=\"{{ $category-&gt;value }}\"&gt;{{ $category-&gt;label() }}&lt;\/option&gt;<\/strong>\r\n<strong>                    @endforeach<\/strong>\r\n<strong>                &lt;\/select&gt;<\/strong>\r\n\r\n                @error('category_id')\r\n                    &lt;div class=\"alert alert-danger\"&gt;{{ $message }}&lt;\/div&gt;\r\n                @enderror\r\n\r\n                &lt;button type=\"submit\" class=\"btn btn-primary mt-3\"&gt;\u9001\u4fe1\u3059\u308b&lt;\/button&gt;\r\n\r\n            &lt;\/form&gt;\r\n\r\n        &lt;\/div&gt;\r\n    &lt;\/div&gt;\r\n&lt;\/div&gt;\r\n&lt;\/body&gt;\r\n&lt;\/html&gt;<\/pre>\n<p>\u3053\u306e\u4e2d\u3067\u91cd\u8981\u306a\u306e\u304c\u3001\u30bb\u30ec\u30af\u30c8\u30dc\u30c3\u30af\u30b9\u3092\u30eb\u30fc\u30d7\u3067\u3064\u304f\u3063\u3066\u3044\u308b\u3068\u3053\u308d\u3067\u3059\u3002<\/p>\n<p>\u3064\u307e\u308a\u3001\u4eca\u56de\u306e\u5834\u5408\u3067\u8a00\u3046\u3068\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u3059\u308b\u3053\u3068\u3067\u305d\u308c\u305e\u308c\u306e\u5024\u3092\u53d6\u5f97\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n<ul>\n<li>$category-&gt;name\uff1a Book\u3001Magazine\u3001Newspaper<\/li>\n<li>$category-&gt;value\uff1a 1\u30012\u30013<\/li>\n<li>$category-&gt;label()\uff1a \u672c\u3001\u96d1\u8a8c\u3001\u65b0\u805e<\/li>\n<\/ul>\n<p>\u305d\u3057\u3066\u3001\u3053\u308c\u3092\u4f7f\u3048\u3070<code>JavaScript<\/code>\u5074\u306b\u3082\u914d\u5217\u3084\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3067<code>Enum<\/code>\u306e\u4e2d\u8eab\u3092\u4f7f\u3048\u308b\u3088\u3046\u306b\u306a\u308b\u3068\u3044\u3046\u308f\u3051\u3067\u3059\u306d\u3002<\/p>\n<h1>\u30eb\u30fc\u30c8\u3092\u3064\u304f\u308b<\/h1>\n<p>\u3067\u306f\u3001\u30b3\u30f3\u30c8\u30ed\u30fc\u30e9\u30fc\u3067\u3064\u304f\u3063\u305f\u30e1\u30bd\u30c3\u30c9\u3092\u30eb\u30fc\u30c8\u3068\u3057\u3066\u767b\u9332\u3057\u3066\u304a\u304d\u307e\u3057\u3087\u3046\u3002<\/p>\n<pre><strong>use App\\Http\\Controllers\\EnumController;<\/strong>\r\n\r\n\/\/ \u7701\u7565\r\n\r\n<strong>Route::get('enum\/create', [EnumController::class, 'create']);<\/strong>\r\n<strong>Route::post('enum', [EnumController::class, 'store']);<\/strong><\/pre>\n<p>\u3053\u308c\u3067\u4f5c\u696d\u306f\u5b8c\u4e86\u3067\u3059\u2757<\/p>\n<p>\u304a\u75b2\u308c\u69d8\u3067\u3057\u305f\ud83d\ude04\u2728<\/p>\n<h1>\u3061\u306a\u307f\u306b\uff1a JavaScript \u5074\u3078\u30bb\u30c3\u30c8\u3059\u308b\u306b\u306f<\/h1>\n<p>\u3061\u306a\u307f\u306b\u3001\u4f8b\u3048\u3070<code>Vue 3<\/code>\u306b<code>Enum<\/code>\u306e\u4e2d\u8eab\u3092\u30bb\u30c3\u30c8\u3059\u308b\u5834\u5408\u306f\u6b21\u306e\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<pre>Vue.createApp({\r\n    data() {\r\n        return {\r\n            <strong>categories: [<\/strong>\r\n<strong>                @foreach($categories as $category)<\/strong>\r\n<strong>                {<\/strong>\r\n<strong>                    id: {{ $category-&gt;value }},<\/strong>\r\n<strong>                    name: '{{ $category-&gt;name }}',<\/strong>\r\n<strong>                    label: '{{ $category-&gt;label() }}'<\/strong>\r\n<strong>                },<\/strong>\r\n<strong>                @endforeach<\/strong>\r\n<strong>            ]<\/strong>\r\n        }\r\n    },\r\n}).mount('#app');<\/pre>\n<p>\u307e\u305f\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306b<code>key-value<\/code>\u5f62\u5f0f\u306e\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306b\u3059\u308b\u3053\u3068\u3082\u3067\u304d\u307e\u3059\u3002<\/p>\n<pre>Vue.createApp({\r\n    data() {\r\n        return {\r\n            <strong>categories: {<\/strong>\r\n<strong>                @foreach($categories as $category)<\/strong>\r\n<strong>                    {{ $category-&gt;value }}: '{{ $category-&gt;label() }}',<\/strong>\r\n<strong>                @endforeach<\/strong>\r\n<strong>            }<\/strong>\r\n        }\r\n    },\r\n}).mount('#app');<\/pre>\n<h1>\u30c6\u30b9\u30c8\u3057\u3066\u307f\u308b<\/h1>\n<p>\u3067\u306f\u3001\u5b9f\u969b\u306b\u30c6\u30b9\u30c8\u3057\u3066\u307f\u307e\u3057\u3087\u3046\u2757<\/p>\n<p>\u300c<strong>https:\/\/******\/enum\/create<\/strong>\u300d\u3078\u30d6\u30e9\u30a6\u30b6\u304b\u3089\u30a2\u30af\u30bb\u30b9\u3057\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-9830\" style=\"border: 3px solid #000;\" src=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2021\/11\/laravel_enum_1.png\" alt=\"\" width=\"633\" height=\"267\" srcset=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2021\/11\/laravel_enum_1.png 633w, https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2021\/11\/laravel_enum_1-300x127.png 300w\" sizes=\"auto, (max-width: 633px) 100vw, 633px\" \/><\/p>\n<p>\u3059\u308b\u3068\u3001\u30bb\u30ec\u30af\u30c8\u30dc\u30c3\u30af\u30b9\u304c\u8868\u793a\u3055\u308c\u308b\u306e\u3067\u3001\u3053\u306e\u4e2d\u304b\u3089\u300c<strong>\u672c<\/strong>\u300d\u3092\u9078\u629e\u80a2\u3001\u9001\u4fe1\u3057\u3066\u307f\u307e\u3057\u3087\u3046\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-9831\" style=\"border: 3px solid #000;\" src=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2021\/11\/laravel_enum_2.png\" alt=\"\" width=\"434\" height=\"139\" srcset=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2021\/11\/laravel_enum_2.png 434w, https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2021\/11\/laravel_enum_2-300x96.png 300w\" sizes=\"auto, (max-width: 434px) 100vw, 434px\" \/><\/p>\n<p>\u3059\u308b\u3068\u30fb\u30fb\u30fb\u30fb\u30fb\u30fb<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-9832\" style=\"border: 3px solid #000;\" src=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2021\/11\/laravel_enum_3.png\" alt=\"\" width=\"248\" height=\"45\" \/><\/p>\n<p>\u306f\u3044\u2757<br \/>\n\u3046\u307e\u304f\u30d0\u30ea\u30c7\u30fc\u30b7\u30e7\u30f3\u3092\u901a\u904e\u3067\u304d\u307e\u3057\u305f\u3002<\/p>\n<p>\u305f\u3060\u3001\u3053\u308c\u3060\u3051\u3067\u306f\u672c\u5f53\u306b\u3061\u3083\u3093\u3068<code>Enum<\/code>\u4ee5\u5916\u306e\u3082\u306e\u3092\u62d2\u5426\u3057\u3066\u304f\u308c\u308b\u306e\u304b\u308f\u304b\u3089\u306a\u3044\u306e\u3067\u3001\u6b21\u306b\u9078\u629e\u80a2\u306b\u306a\u3044\u300c\u6280\u8853\u66f8\u300d\u3092\u308f\u3056\u3068\u30bb\u30ec\u30af\u30c8\u30dc\u30c3\u30af\u30b9\u306b\u8ffd\u52a0\u3057\u3066\u9001\u4fe1\u3057\u3066\u307f\u307e\u3057\u3087\u3046\u3002<\/p>\n<pre>&lt;select name=\"category_id\" class=\"form-control\"&gt;\r\n    &lt;option value=\"\"&gt;\u25bc \u4ee5\u4e0b\u304b\u3089\u9078\u629e&lt;\/option&gt;\r\n    @foreach($categories as $category)\r\n        &lt;option value=\"{{ $category-&gt;value }}\"&gt;{{ $category-&gt;label() }}&lt;\/option&gt;\r\n    @endforeach\r\n    <strong>&lt;!-- \u26a0 \u3053\u308c\u306f Enum \u306b\u306f\u5165\u3063\u3066\u3044\u306a\u3044\u9078\u629e\u80a2\u3067\u3059\uff01 --&gt;<\/strong>\r\n<strong>    &lt;option value=\"999\"&gt;\u6280\u8853\u66f8&lt;\/option&gt;\r\n<\/strong>\r\n&lt;\/select&gt;<\/pre>\n<p>\u3067\u306f\u3001\u3082\u3046\u4e00\u5ea6\u30ea\u30ed\u30fc\u30c9\u3057\u3066\u9078\u629e\u3057\u306a\u304a\u3057\u307e\u3059\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-9833\" style=\"border: 3px solid #000;\" src=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2021\/11\/laravel_enum_4.png\" alt=\"\" width=\"417\" height=\"129\" srcset=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2021\/11\/laravel_enum_4.png 417w, https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2021\/11\/laravel_enum_4-300x93.png 300w\" sizes=\"auto, (max-width: 417px) 100vw, 417px\" \/><\/p>\n<p>\u3069\u3046\u306a\u3063\u305f\u3067\u3057\u3087\u3046\u304b\u30fb\u30fb\u30fb\u30fb\u30fb\u30fb<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-9834\" style=\"border: 3px solid #000;\" src=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2021\/11\/laravel_enum_5.png\" alt=\"\" width=\"431\" height=\"207\" srcset=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2021\/11\/laravel_enum_5.png 431w, https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2021\/11\/laravel_enum_5-300x144.png 300w\" sizes=\"auto, (max-width: 431px) 100vw, 431px\" \/><\/p>\n<p>\u306f\u3044\u2757<\/p>\n<p>\u30d0\u30ea\u30c7\u30fc\u30b7\u30e7\u30f3\u30fb\u30e1\u30c3\u30bb\u30fc\u30b8\u306f\u82f1\u8a9e\u306e\u307e\u307e\u3067\u3059\u304c\u3001\u3046\u307e\u304f\u30a8\u30e9\u30fc\u3092\u8868\u793a\u3057\u3066\u304f\u308c\u307e\u3057\u305f\u3002<\/p>\n<p>\u6210\u529f\u3067\u3059\ud83d\ude04\u2728<\/p>\n<h1>\u4f01\u696d\u69d8\u3078\u306e\u3054\u63d0\u6848<\/h1>\n<p>\u4eca\u56de\u306e\u3088\u3046\u306b\u3001\u300c<strong>\u3053\u308c\u3057\u304b\u3042\u308a\u3048\u306a\u3044\u9078\u629e\u80a2<\/strong>\u300d\u3092\u4f7f\u3046\u3053\u3068\u3067\u3001\u3088\u308a\u60f3\u5b9a\u5916\u306e\u51fa\u6765\u4e8b\u304c\u8d77\u304d\u306b\u304f\u3044\u30b3\u30fc\u30c9\u3092\u66f8\u304f\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n<p>\u307e\u305f\u3001<code>Enum<\/code>\u306f\u4f7f\u3044\u56de\u3057\u3082\u3057\u3084\u3059\u3044\u3067\u3059\u306e\u3067\u3001\u958b\u767a\u306e\u52b9\u7387\u5316\u306b\u3082\u5f79\u7acb\u3064\u304b\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n<p>\u3082\u3057\u305d\u3046\u3044\u3063\u305f\u3054\u4f9d\u983c\u304c\u3054\u3056\u3044\u307e\u3057\u305f\u3089\u3001\u304a\u6c17\u8efd\u306b\u304a\u554f\u3044\u5408\u308f\u305b\u304b\u3089\u3054\u9023\u7d61\u304f\u3060\u3055\u3044\u3002<\/p>\n<p>\u3069\u3046\u305e\u3088\u308d\u3057\u304f\u304a\u9858\u3044\u3044\u305f\u3057\u307e\u3059\u3002m(_ _)m<\/p>\n<h1>\u304a\u308f\u308a\u306b<\/h1>\n<p>\u3068\u3044\u3046\u3053\u3068\u3067\u3001\u4eca\u56de\u306f<code>PHP 8.1<\/code>\u304b\u3089\u4f7f\u3048\u308b\u3088\u3046\u306b\u306a\u308b<code>Enum<\/code>\u3092<code>Laravel<\/code>\u3067\u4f7f\u3063\u3066\u307f\u307e\u3057\u305f\u3002<\/p>\n<p>\u3053\u308c\u307e\u3067\u30b0\u30eb\u30fc\u30d7\u5316\u3055\u308c\u305f\u5b9a\u6570\u3092\u4f7f\u3046\u5834\u5408\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u30e2\u30c7\u30eb\u306e\u4e2d\u306b\u5165\u308c\u308b\u30d1\u30bf\u30fc\u30f3\u3084\u3001<code>Laravel<\/code>\u306e<code>config<\/code>\u30d5\u30a9\u30eb\u30c0\u306e\u4e2d\u306b\u30bb\u30c3\u30c8\u3059\u308b\u30d1\u30bf\u30fc\u30f3\u304c\u591a\u304b\u3063\u305f\u304b\u3082\u3057\u308c\u307e\u305b\u3093\u304c\u3001\u3053\u308c\u304b\u3089\u306f<code>Enum<\/code>\u3092\u4f7f\u3046\u306e\u3082\u3042\u308a\u3058\u3083\u306a\u3044\u3067\u3057\u3087\u3046\u304b\u3002<\/p>\n<pre>class Category extends Model\r\n{\r\n    use HasFactory;\r\n\r\n    <strong>const BOOK = 1;<\/strong>\r\n<strong>    const MAGAZINE = 2;<\/strong>\r\n<strong>    const NEWSPAPER = 3;<\/strong>\r\n\r\n<strong>    const CATEGORIES = [<\/strong>\r\n<strong>        self::BOOK =&gt; '\u672c',<\/strong>\r\n<strong>        self::MAGAZINE =&gt; '\u96d1\u8a8c',<\/strong>\r\n<strong>        self::NEWSPAPER =&gt; '\u65b0\u805e',<\/strong>\r\n<strong>    ];<\/strong>\r\n}<\/pre>\n<p>\u305c\u3072\u3001\u307f\u306a\u3055\u3093\u3082\u4e00\u5ea6\u8a66\u3057\u3066\u307f\u3066\u304f\u3060\u3055\u3044\u306d\u3002<\/p>\n<p>\u3067\u306f\u3067\u306f\u301c\u2757<\/p>\n<p style=\"text-align: center;\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-9838\" style=\"border: 3px solid #000;\" src=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2021\/11\/zasetsu_kinshi.jpg\" alt=\"\" width=\"200\" height=\"255\" \/><\/p>\n<p style=\"text-align: center;\">\u300c\u3053\u306e\u6a19\u8b58\u306b\u52c7\u6c17\u3065\u3051\u3089\u308c\u307e\u3057\u305f\u3002<br \/>\n\u632b\u6298\u7981\u6b62 \ud83d\ude02\u2728\u300d<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u3055\u3066\u3055\u3066\u3001\u300c\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u8a00\u8a9e\u300d\u3068\u4e00\u53e3\u306b\u8a00\u3063\u3066\u3082\u4e16\u306e\u4e2d\u306b\u306fPHP\u4ee5\u5916\u306b\u3082Pyhon\u3084Java\u306a\u3069\u305f\u304f\u3055\u3093\u306e\u3082\u306e\u304c\u5b58\u5728\u3057\u3066\u3044\u307e\u3059\u3002 \u305d\u3057\u3066\u3001\u305d\u3093\u306a\u4ed6\u306e\u8a00\u8a9e\u3092\u4f7f\u3063\u305f &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/blog.capilano-fw.com\/?p=9829\" class=\"more-link\"><span class=\"screen-reader-text\">&#8220;\u3010Laravel\u3011PHP 8.1 \u304b\u3089\u4f7f\u3048\u308b Enum\u3067\u3001\u9078\u629e\u80a2\u301c\u30d0\u30ea\u30c7\u30fc\u30b7\u30e7\u30f3\u307e\u3067\u5b9f\u88c5\u3057\u3066\u307f\u308b&#8221; \u306e<\/span>\u7d9a\u304d\u3092\u8aad\u3080<\/a><\/p>\n","protected":false},"author":1,"featured_media":9841,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-9829","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-laravel"],"_links":{"self":[{"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=\/wp\/v2\/posts\/9829","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=9829"}],"version-history":[{"count":6,"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=\/wp\/v2\/posts\/9829\/revisions"}],"predecessor-version":[{"id":9842,"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=\/wp\/v2\/posts\/9829\/revisions\/9842"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=\/wp\/v2\/media\/9841"}],"wp:attachment":[{"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=9829"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=9829"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=9829"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}