{"id":8195,"date":"2021-01-18T07:00:20","date_gmt":"2021-01-17T22:00:20","guid":{"rendered":"https:\/\/blog.capilano-fw.com\/?p=8195"},"modified":"2021-01-01T00:48:48","modified_gmt":"2020-12-31T15:48:48","slug":"laravel%e3%81%ae%e3%83%90%e3%83%aa%e3%83%87%e3%83%bc%e3%82%b7%e3%83%a7%e3%83%b3%e3%83%bb%e3%82%a8%e3%83%a9%e3%83%bc%e3%83%a1%e3%83%83%e3%82%bb%e3%83%bc%e3%82%b8%e3%82%92%e3%82%b7%e3%83%b3%e3%83%97","status":"publish","type":"post","link":"https:\/\/blog.capilano-fw.com\/?p=8195","title":{"rendered":"Laravel\u306e\u30d0\u30ea\u30c7\u30fc\u30b7\u30e7\u30f3\u30fb\u30a8\u30e9\u30fc\u30e1\u30c3\u30bb\u30fc\u30b8\u3092\u30b7\u30f3\u30d7\u30eb\u69cb\u6210\u306b\u3059\u308b"},"content":{"rendered":"<p>\u3055\u3066\u3055\u3066\u3001<code>Laravel<\/code>\u304c\u3053\u308c\u307b\u3069\u4eba\u6c17\u306e\u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af\u306b\u306a\u3063\u305f\u306e\u306f\u3044\u308d\u3044\u308d\u3068\u7406\u7531\u304c\u3042\u308b\u3068\u601d\u3044\u307e\u3059\u304c\u3001\u500b\u4eba\u7684\u306b\u305d\u306e\u3072\u3068\u3064\u3068\u306b\u300c<strong>\u624b\u8efd\u306b\u30d0\u30ea\u30c7\u30fc\u30b7\u30e7\u30f3\uff08\u5165\u529b\u30c1\u30a7\u30c3\u30af\uff09\u304c\u4f7f\u3048\u308b<\/strong>\u300d\u3068\u3044\u3046\u306e\u304c\u5165\u3063\u3066\u3044\u307e\u3059\u3002<\/p>\n<p>\u305f\u3060\u3001\u305d\u3053\u306b\u4e00\u70b9\u3060\u3051\u300c<strong>\u3053\u3046\u3067\u304d\u305f\u3089\u3044\u3044\u306a<\/strong>\u300d\u3068\u601d\u3046\u3053\u3068\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n<p>\u305d\u308c\u306f\u30fb\u30fb\u30fb\u30fb\u30fb\u30fb<\/p>\n<p><strong style=\"font-size: 35px;\">\u30d0\u30ea\u30c7\u30fc\u30b7\u30e7\u30f3\u30fb\u30a8\u30e9\u30fc\u306e\u69cb\u6210<\/strong><\/p>\n<p>\u3067\u3059\u3002<\/p>\n<p>\u3053\u308c\u306f\u3001\u6700\u8fd1\u30e1\u30a4\u30f3\u3067\u4f7f\u3046\u3088\u3046\u306b\u306a\u3063\u305f<code>Ajax\u9001\u4fe1<\/code>\u306e\u8a71\u306a\u306e\u3067\u3059\u304c\u3001\u30d0\u30ea\u30c7\u30fc\u30b7\u30e7\u30f3\u30fb\u30a8\u30e9\u30fc\u304c\u767a\u751f\u3059\u308b\u3068\u6b21\u306e\u3088\u3046\u306a\u30ec\u30b9\u30dd\u30f3\u30b9\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-8196\" style=\"border: 3px solid #000;\" src=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2020\/10\/laravel_validation_error_structure_1.png\" alt=\"\" width=\"432\" height=\"175\" srcset=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2020\/10\/laravel_validation_error_structure_1.png 432w, https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2020\/10\/laravel_validation_error_structure_1-300x122.png 300w\" sizes=\"auto, (max-width: 432px) 100vw, 432px\" \/><\/p>\n<p>\u898b\u3066\u3044\u305f\u3060\u304f\u3068\u308f\u304b\u308b\u3068\u304a\u308a\u3001<strong>\u5404\u30ad\u30fc\u3054\u3068\u306b\u30a8\u30e9\u30fc\u304c\u300c\u914d\u5217\u300d\u3067\u683c\u7d0d\u3055\u308c\u3066\u3044\u307e\u3059\u306d\u3002<\/strong><\/p>\n<p>\u3053\u308c\u306f\u3001\u8907\u6570\u306e\u30d0\u30ea\u30c7\u30fc\u30b7\u30e7\u30f3\u30fb\u30a8\u30e9\u30fc\u304c\u767a\u751f\u3059\u308b\u3053\u3068\u3092\u60f3\u5b9a\u3057\u3066\u3044\u308b\u304b\u3089\u3067\u3059\u3002<\/p>\n<p>\u305f\u3060\u3001\u3082\u3061\u308d\u3093\u3053\u308c\u304c\u6b63\u3057\u3044\u306e\u3067\u3059\u304c\u3001\u500b\u4eba\u7684\u306b\u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u3082\u3063\u3068\u30b7\u30f3\u30d7\u30eb\u8fd4\u3063\u3066\u6765\u3066\u307b\u3057\u3044\u6642\u3082\u7d50\u69cb\u3042\u3063\u305f\u308a\u3057\u307e\u3059\u3002<\/p>\n<p>\u2193\u2193\u2193 \u3053\u3093\u306a\u611f\u3058\u3067\u3059\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-8198\" style=\"border: 3px solid #000;\" src=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2020\/10\/laravel_validation_error_structure_2.png\" alt=\"\" width=\"376\" height=\"86\" srcset=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2020\/10\/laravel_validation_error_structure_2.png 376w, https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2020\/10\/laravel_validation_error_structure_2-300x69.png 300w\" sizes=\"auto, (max-width: 376px) 100vw, 376px\" \/><\/p>\n<p>\u3068\u3044\u3046\u306e\u3082\u3001\u305d\u3082\u305d\u3082\u30a8\u30e9\u30fc\u304c\u8907\u6570\u5165\u3063\u3066\u308b\u3053\u3068\u306f\u5c11\u306a\u3044\u3067\u3059\u3057\u3001\u8907\u6570\u30a8\u30e9\u30fc\u3092\u5168\u3066\u8868\u793a\u3057\u3066\u3057\u307e\u3046\u3068\u3001\u30c6\u30ad\u30b9\u30c8\u304c\u591a\u3059\u304e\u3066\u9006\u306b\u5185\u5bb9\u3092\u628a\u63e1\u3057\u306b\u304f\u3044\u3088\u3046\u306a\u6c17\u304c\u3059\u308b\u3093\u3067\u3059\u306d\u3002\uff08\u597d\u307f\u306e\u554f\u984c\u3082\u3042\u308b\u3093\u3067\u3057\u3087\u3046\u3051\u3069\u3001<code>@error<\/code>\u306e\u5834\u5408\u3082\u30a8\u30e9\u30fc\u306f\u3072\u3068\u3064\u3067\u3059\u3088\u306d\uff09<\/p>\n<p>\u2193\u2193\u2193 \u3053\u3093\u306a\u611f\u3058\u3067\u3059\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-8200\" style=\"border: 3px solid #000;\" src=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2020\/10\/laravel_validation_error_structure_3.png\" alt=\"\" width=\"426\" height=\"201\" srcset=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2020\/10\/laravel_validation_error_structure_3.png 426w, https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2020\/10\/laravel_validation_error_structure_3-300x142.png 300w\" sizes=\"auto, (max-width: 426px) 100vw, 426px\" \/><\/p>\n<p>\u305d\u3053\u3067\u2757<\/p>\n<p>\u4eca\u56de\u306f<code>Laravel<\/code>\u306e\u30d0\u30ea\u30c7\u30fc\u30b7\u30e7\u30f3\u30a8\u30e9\u30fc\u306e\u69cb\u6210\u3092\u30b7\u30f3\u30d7\u30eb\u306b\u30ab\u30b9\u30bf\u30de\u30a4\u30ba\u3059\u308b\u65b9\u6cd5\u3092\u3054\u7d39\u4ecb\u3057\u307e\u3059\u3002<\/p>\n<p>\u305c\u3072\u7686\u3055\u3093\u306e\u53c2\u8003\u306b\u306a\u308c\u3070\u5b09\u3057\u3044\u3067\u3059\ud83d\ude0a\u2728<\/p>\n<p style=\"text-align: center;\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-6070 aligncenter\" src=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2020\/03\/no_5.png\" alt=\"\" width=\"260\" height=\"320\" srcset=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2020\/03\/no_5.png 260w, https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2020\/03\/no_5-244x300.png 244w\" sizes=\"auto, (max-width: 260px) 100vw, 260px\" \/>\u300c\u51ac\u4ee5\u5916\u306f\u3001\u5bdd\u888b\u3067\u5bdd\u3066\u3044\u307e\u3059\ud83d\udc4d\u300d<\/p>\n<p><strong>\u958b\u767a\u74b0\u5883\uff1a<\/strong> Laravel 8.x\u3001Vue 3<\/p>\n<h1>Ajax\u9001\u4fe1\u3059\u308b\u30c6\u30b9\u30c8\u74b0\u5883\u3092\u3064\u304f\u308b<\/h1>\n<p>\u307e\u305a\u3001\u4eca\u56de\u30c7\u30fc\u30bf\u9001\u4fe1\u3057\u3066\u30d0\u30ea\u30c7\u30fc\u30b7\u30e7\u30f3\u3092\u5b9f\u884c\u3067\u304d\u308b\u30c6\u30b9\u30c8\u74b0\u5883\u3092\u3064\u304f\u3063\u3066\u3044\u304d\u307e\u3059\u3002\uff08\u4e0d\u8981\u306a\u65b9\u306f\u6b21\u306e\u9805\u76ee\u307e\u3067\u98db\u3070\u3057\u3066\u304f\u3060\u3055\u3044\uff09<\/p>\n<p>\u306a\u304a\u3001\u4eca\u56de\u306f\u30c6\u30b9\u30c8\u3068\u3057\u3066<code>items<\/code>\u30c6\u30fc\u30d6\u30eb\u306b\u30c7\u30fc\u30bf\u4fdd\u5b58\u3059\u308b\u5834\u5408\u3092\u60f3\u5b9a\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n<h2>\u30b3\u30f3\u30c8\u30ed\u30fc\u30e9\u30fc\u3092\u3064\u304f\u308b<\/h2>\n<p>\u307e\u305a\u306f\u30b3\u30f3\u30c8\u30ed\u30fc\u30e9\u30fc\u3067\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 ItemController<\/strong><\/pre>\n<p>\u3059\u308b\u3068\u30d5\u30a1\u30a4\u30eb\u304c\u4f5c\u6210\u3055\u308c\u308b\u306e\u3067\u3001\u4e2d\u8eab\u3092\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u5909\u66f4\u3057\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n<p><strong>app\/Http\/Controllers\/ItemController.php<\/strong><\/p>\n<pre>&lt;?php\r\n\r\nnamespace App\\Http\\Controllers;\r\n\r\nuse App\\Http\\Requests\\ItemRequest;\r\nuse Illuminate\\Http\\Request;\r\n\r\nclass ItemController extends Controller\r\n{\r\n    <strong>public function create() {<\/strong>\r\n\r\n<strong>        return view('item.create');<\/strong>\r\n\r\n<strong>    }<\/strong>\r\n\r\n<strong>    public function store(ItemRequest $request) {<\/strong>\r\n\r\n<strong>        \/\/ \u3053\u3053\u3067\u30c7\u30fc\u30bf\u3092\u4fdd\u5b58<\/strong>\r\n\r\n<strong>    }<\/strong>\r\n}<\/pre>\n<h2>\u30d3\u30e5\u30fc\u3092\u3064\u304f\u308b<\/h2>\n<p>\u7d9a\u3044\u3066\u30d3\u30e5\u30fc\u3067\u3059\u3002<br \/>\n\u4f5c\u6210\u306e\u30b3\u30de\u30f3\u30c9\u306f\u306a\u3044\u306e\u3067\u3001\u76f4\u63a5\u30d5\u30a1\u30a4\u30eb\u3092\u4f5c\u6210\u3057\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n<p><strong>resources\/views\/item\/create.blade.php<\/strong><\/p>\n<pre>&lt;html&gt;\r\n&lt;head&gt;\r\n    &lt;link rel=\"stylesheet\" href=\"https:\/\/stackpath.bootstrapcdn.com\/bootstrap\/4.5.0\/css\/bootstrap.min.css\"&gt;\r\n&lt;\/head&gt;\r\n&lt;body&gt;\r\n&lt;div id=\"app\" class=\"p-5\"&gt;\r\n    <strong>&lt;div class=\"form-group\"&gt;<\/strong>\r\n<strong>        &lt;label&gt;\u540d\u524d&lt;\/label&gt;<\/strong>\r\n<strong>        &lt;input type=\"text\" class=\"form-control\" v-model=\"params.name\"&gt;<\/strong>\r\n<strong>    &lt;\/div&gt;<\/strong>\r\n<strong>    &lt;div class=\"form-group\"&gt;<\/strong>\r\n<strong>        &lt;label&gt;\u8aac\u660e\u6587&lt;\/label&gt;<\/strong>\r\n<strong>        &lt;textarea type=\"text\" class=\"form-control\" v-model=\"params.description\"&gt;&lt;\/textarea&gt;<\/strong>\r\n<strong>    &lt;\/div&gt;<\/strong>\r\n<strong>    &lt;div class=\"form-group\"&gt;<\/strong>\r\n<strong>        &lt;label&gt;\u4fa1\u683c&lt;\/label&gt;<\/strong>\r\n<strong>        &lt;input type=\"number\" min=\"0\" class=\"form-control\" v-model=\"params.price\"&gt;<\/strong>\r\n<strong>    &lt;\/div&gt;<\/strong>\r\n<strong>    &lt;div class=\"form-group\"&gt;<\/strong>\r\n<strong>        &lt;button type=\"button\" class=\"btn btn-primary\" @click=\"onSubmit\"&gt;\u9001\u4fe1&lt;\/button&gt;<\/strong>\r\n<strong>    &lt;\/div&gt;<\/strong>\r\n&lt;\/div&gt;\r\n&lt;script src=\"https:\/\/code.jquery.com\/jquery-3.5.1.min.js\"&gt;&lt;\/script&gt;\r\n&lt;script src=\"https:\/\/stackpath.bootstrapcdn.com\/bootstrap\/4.5.0\/js\/bootstrap.min.js\"&gt;&lt;\/script&gt;\r\n&lt;script src=\"https:\/\/unpkg.com\/vue@next\"&gt;&lt;\/script&gt;\r\n&lt;script src=\"https:\/\/cdnjs.cloudflare.com\/ajax\/libs\/axios\/0.19.2\/axios.min.js\"&gt;&lt;\/script&gt;\r\n&lt;script&gt;\r\n\r\n    Vue.createApp({\r\n        el: '#app',\r\n        data() {\r\n            return {\r\n                params: {\r\n                    name: '',\r\n                    description: '',\r\n                    price: 0\r\n                }\r\n            }\r\n        },\r\n        methods: {\r\n            onSubmit() {\r\n\r\n                const url = '\/item';\r\n                axios.post(url, this.params)\r\n                    .then(response =&gt; {\r\n\r\n                        \/\/ \u6210\u529f\u3057\u305f\u6642\r\n\r\n                    })\r\n                    .catch(error =&gt; {\r\n\r\n                        \/\/ \u5931\u6557\u3057\u305f\u6642\r\n\r\n                    });\r\n\r\n            }\r\n        }\r\n    }).mount('#app');\r\n\r\n&lt;\/script&gt;\r\n&lt;\/body&gt;\r\n&lt;\/html&gt;<\/pre>\n<h2>\u30eb\u30fc\u30c8\u3092\u3064\u304f\u308b<\/h2>\n<p>\u6700\u5f8c\u306b\u30eb\u30fc\u30c8\u3067\u3059\u3002<\/p>\n<p><strong>routes\/web.php<\/strong><\/p>\n<pre>&lt;?php\r\n\r\n\/\/ \u7701\u7565\r\n\r\n\/\/ \ud83d\udc47 \u4ee5\u4e0b\u3092\u8ffd\u52a0\r\n<strong>Route::get('item\/create', [\\App\\Http\\Controllers\\ItemController::class, 'create']);<\/strong>\r\n<strong>Route::post('item', [\\App\\Http\\Controllers\\ItemController::class, 'store']);<\/strong><\/pre>\n<p>\u3053\u308c\u3067\u30c6\u30b9\u30c8\u74b0\u5883\u304c\u3067\u304d\u3042\u304c\u308a\u307e\u3057\u305f\ud83d\ude0a<\/p>\n<h1>\u72ec\u81ea\u306e\u30ea\u30af\u30a8\u30b9\u30c8\u3092\u3064\u304f\u308b<\/h1>\n<p>\u3067\u306f\u3001\u3053\u3053\u304b\u3089\u304c\u300c<strong>\u30b7\u30f3\u30d7\u30eb\u306a\u30d0\u30ea\u30c7\u30fc\u30b7\u30e7\u30f3\u30fb\u30a8\u30e9\u30fc<\/strong>\u300d\u3092\u3064\u304f\u308b\u90e8\u5206\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<p>\u307e\u305a\u3001\u4ee5\u4e0b\u306e\u30b3\u30de\u30f3\u30c9\u3067\u72ec\u81ea\u306e<code>FormRequest<\/code>\u3092\u3064\u304f\u308a\u307e\u3059\u3002<\/p>\n<pre><strong>php artisan make:request ItemRequest<\/strong><\/pre>\n<p>\u3059\u308b\u3068\u30ea\u30af\u30a8\u30b9\u30c8\u30fb\u30d5\u30a1\u30a4\u30eb\u304c\u4f5c\u6210\u3055\u308c\u308b\u306e\u3067\u4e2d\u8eab\u3092\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u5909\u66f4\u3057\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n<p><strong>app\/Http\/Requests\/ItemRequest.php<\/strong><\/p>\n<pre>&lt;?php\r\n\r\nnamespace App\\Http\\Requests;\r\n\r\nuse Illuminate\\Contracts\\Validation\\Validator;\r\nuse Illuminate\\Foundation\\Http\\FormRequest;\r\nuse Illuminate\\Http\\Exceptions\\HttpResponseException;\r\n\r\nclass ItemRequest extends FormRequest\r\n{\r\n    public function authorize()\r\n    {\r\n        <strong>return true<\/strong>; \/\/ \ud83d\udc48 \u4eca\u56de\u3001\u8a8d\u8a3c\u30c1\u30a7\u30c3\u30af\u306f\u3057\u306a\u3044\u306e\u3067 true \u3078\u5909\u66f4\u3002\r\n    }\r\n\r\n    public function rules()\r\n    {\r\n        \/\/ \ud83d\udc47 \u3053\u3053\u304c\u30c1\u30a7\u30c3\u30af\u3059\u308b\u30d0\u30ea\u30c7\u30fc\u30b7\u30e7\u30f3\u30fb\u30eb\u30fc\u30eb\u3067\u3059\u3002\u304a\u597d\u307f\u3067\u5909\u66f4\u3057\u3066\u304f\u3060\u3055\u3044\u3002\r\n        <strong>return [<\/strong>\r\n<strong>            'name' =&gt; ['required'],<\/strong>\r\n<strong>            'description' =&gt; ['required'],<\/strong>\r\n<strong>            'price' =&gt; ['required', 'integer', 'min:1']<\/strong>\r\n<strong>        ];<\/strong>\r\n    }\r\n\r\n    \/\/ Override\r\n    <strong>protected function failedValidation(Validator $validator)<\/strong>\r\n<strong>    {<\/strong>\r\n<strong>        $errors = collect($validator-&gt;errors());<\/strong>\r\n<strong>        $messages = $errors-&gt;map(function($error_messages){<\/strong>\r\n\r\n<strong>            return $error_messages[0];<\/strong>\r\n\r\n<strong>        });<\/strong>\r\n\r\n<strong>        throw new HttpResponseException(response(<\/strong>\r\n<strong>            $messages,<\/strong>\r\n<strong>            422<\/strong>\r\n<strong>        ));<\/strong>\r\n<strong>    }<\/strong>\r\n}<\/pre>\n<p>\u3053\u3053\u3067\u91cd\u8981\u306a\u306e\u304c\u3001<code>failedValidation()<\/code>\u3067\u3059\u3002<\/p>\n<p>\u3053\u306e\u30e1\u30bd\u30c3\u30c9\u306f<strong>\u30d0\u30ea\u30c7\u30fc\u30b7\u30e7\u30f3\u304c\u5931\u6557\u3057\u305f\u6642\u306b\u547c\u3070\u308c\u308b<\/strong>\u3053\u3068\u306b\u306a\u3063\u3066\u3044\u3066\u3001\u3053\u3053\u3092\u4e0a\u66f8\u304d\u3059\u308b\u3053\u3068\u3067\u300c\u30b7\u30f3\u30d7\u30eb\u306a\u30a8\u30e9\u30fc\u30e1\u30c3\u30bb\u30fc\u30b8\u300d\u3092\u5b9f\u88c5\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n<p>\u3084\u3063\u3066\u3044\u308b\u624b\u9806\u306f\u4ee5\u4e0b\u306e\u3068\u304a\u308a\u3067\u3059\u3002<\/p>\n<ol>\n<li>Validator\u304b\u3089\u5168\u30a8\u30e9\u30fc\u30e1\u30c3\u30bb\u30fc\u30b8\u3092\u53d6\u5f97<\/li>\n<li>\u305d\u308c\u3089\u306e\u4e2d\u304b\u3089\u3001\u305d\u308c\u305e\u308c\u6700\u521d\u306e\u3082\u306e\u3060\u3051\u3092\u53d6\u5f97<\/li>\n<li>\u53d6\u5f97\u3057\u305f\u30e1\u30c3\u30bb\u30fc\u30b8\u3092\u30ec\u30b9\u30dd\u30f3\u30b9\u3067\u8fd4\u3059<\/li>\n<\/ol>\n<p>\u305f\u3063\u305f\u3053\u308c\u3060\u3051\u3067OK\u3067\u3059\u3002<\/p>\n<h1>\u3088\u308a\u6c4e\u7528\u7684\u306b\u4f7f\u3048\u308b\u3088\u3046\u306b\u3059\u308b<\/h1>\n<p>\u5148\u307b\u3069\u306f\u3001<code>failedValidation()<\/code>\u3092\u72ec\u81ea\u30ea\u30af\u30a8\u30b9\u30c8\u306e\u4e2d\u306b\u76f4\u63a5\u767b\u9332\u3057\u307e\u3057\u305f\u3002<\/p>\n<p>\u3082\u3061\u308d\u3093\u6570\u304c\u5c11\u306a\u3044\u5834\u5408\u306f\u3001\u3053\u308c\u3067\u554f\u984c\u306a\u3044\u306e\u3067\u3059\u304c\u3001\u4eca\u5f8c\u4f5c\u6210\u3059\u308b\u30d0\u30ea\u30c7\u30fc\u30b7\u30e7\u30f3\u306b\u3082\u540c\u3058\u304f\u300c\u30b7\u30f3\u30d7\u30eb\u306a\u30a8\u30e9\u30fc\u30e1\u30c3\u30bb\u30fc\u30b8\u300d\u3092\u5b9f\u88c5\u3059\u308b\u5834\u5408\u3001\u6bce\u56de\u540c\u3058\u30b3\u30fc\u30c9\u3092\u66f8\u304b\u306a\u304f\u3066\u306f\u3044\u3051\u306a\u304f\u306a\u308a\u307e\u3059\u3002<\/p>\n<p>\u3053\u308c\u3067\u306f\u3001\u4fdd\u5b88\u7ba1\u7406\u3082\u96e3\u3057\u304f\u306a\u308a\u307e\u3059\u3057\u3001\u4f55\u3088\u308a\u300c\u3081\u3093\u3069\u304f\u3055\u3044\ud83d\ude2b\u300d\u3067\u3059\u3088\u306d\u3002<\/p>\n<p>\u305d\u3053\u3067\u3001\u305b\u3063\u304b\u304f\u3067\u3059\u306e\u3067\u6c4e\u7528\u7684\u306b\u4f7f\u3048\u308b\u3088\u3046\u306b\u3057\u3066\u307f\u307e\u3057\u3087\u3046\u3002<\/p>\n<p>\u4f7f\u3046\u306e\u306f<code>Trait<\/code>\u3067\u3059\u3002<\/p>\n<p><code>Trait<\/code>\u3068\u3044\u3046\u306e\u306f\u3001\u7c21\u5358\u306b\u3044\u3046\u3068\u8907\u6570\u306e\u30af\u30e9\u30b9\u3067<strong>\u7279\u5b9a\u306e\u30b3\u30fc\u30c9\u3092\u4f7f\u3044\u56de\u3057\u3059\u308b\u305f\u3081\u306e\u3082\u306e<\/strong>\u3067\u3059\u3002<\/p>\n<p>\u3067\u306f\u5b9f\u969b\u306b\u3084\u3063\u3066\u307f\u307e\u3057\u3087\u3046\uff01<br \/>\n\u307e\u305a<code>app\/Traits<\/code>\u30d5\u30a9\u30eb\u30c0\u3092\u3064\u304f\u308a\u3001\u4ee5\u4e0b\u306e\u30d5\u30a1\u30a4\u30eb\u3092\u8ffd\u52a0\u3057\u307e\u3059\u3002<\/p>\n<p><strong>app\/Traits\/SingleValidationErrorMessage.php<\/strong><\/p>\n<pre>&lt;?php\r\n\r\nnamespace App\\Traits;\r\n\r\nuse Illuminate\\Contracts\\Validation\\Validator;\r\nuse Illuminate\\Http\\Exceptions\\HttpResponseException;\r\n\r\n<strong>trait SingleValidationErrorMessage {<\/strong>\r\n\r\n    protected function failedValidation(Validator $validator)\r\n    {\r\n        $errors = collect($validator-&gt;errors());\r\n        $messages = $errors-&gt;map(function($error_messages){\r\n\r\n            return $error_messages[0];\r\n\r\n        });\r\n\r\n        throw new HttpResponseException(response(\r\n            $messages,\r\n            422\r\n        ));\r\n    }\r\n\r\n<strong>}<\/strong><\/pre>\n<p>\u306a\u304a\u3001\u3082\u3061\u308d\u3093<code>failedValidation()<\/code>\u306e\u4e2d\u8eab\u306f\u5148\u307b\u3069\u3068\u5168\u304f\u540c\u3058\u3082\u306e\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<p>\u3067\u306f\u3001\u3053\u308c\u3092\u5148\u307b\u3069\u306e<code>Item.php<\/code>\u3067\u4f7f\u3048\u308b\u3088\u3046\u306b\u3057\u3066\u307f\u307e\u3057\u3087\u3046\u3002<\/p>\n<pre>&lt;?php\r\n\r\nnamespace App\\Http\\Requests;\r\n\r\n<strong>use App\\Traits\\SingleValidationErrorMessage; \/\/ \ud83d\udc48 \u3053\u3053\u3092\u8ffd\u52a0\u3057\u307e\u3057\u305f\u3002<\/strong>\r\nuse Illuminate\\Foundation\\Http\\FormRequest;\r\n\r\nclass ItemRequest extends FormRequest\r\n{\r\n    <strong>use SingleValidationErrorMessage; \/\/ \ud83d\udc48 \u3053\u3053\u3092\u8ffd\u52a0\u3057\u307e\u3057\u305f\u3002<\/strong>\r\n\r\n    public function authorize()\r\n    {\r\n        return true;\r\n    }\r\n\r\n    public function rules()\r\n    {\r\n        return [\r\n            'name' =&gt; ['required'],\r\n            'description' =&gt; ['required'],\r\n            'price' =&gt; ['required', 'integer', 'min:1']\r\n        ];\r\n    }\r\n\r\n    <strong>\/\/ \ud83d\udc49 failedValidation()\u306f\u524a\u9664\u3057\u307e\u3057\u305f<\/strong>\r\n}<\/pre>\n<p>\u3064\u307e\u308a\u3001<code>SingleValidationErrorMessage()<\/code>\u3092\u547c\u3073\u51fa\u3059\u3060\u3051\u3067\u3001<code>failedValidation()<\/code>\u3092\u8ffd\u52a0\u3059\u308b\u5fc5\u8981\u306f\u306a\u304f\u306a\u308b\u3068\u3044\u3046\u3053\u3068\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<p>\u3053\u308c\u3067\u3001\u7c21\u5358\u306b\u4f7f\u3048\u307e\u3059\u3057\u3001\u3082\u3057\u30d0\u30b0\u304c\u3042\u3063\u3066\u3082<code>Trait<\/code>\u306e\u4e2d\u8eab\u3092\u5909\u66f4\u3059\u308c\u3070\u5168\u3066\u306e\u5834\u6240\u306b\u53cd\u6620\u3055\u308c\u308b\u306e\u3067\u7701\u529b\u5316\u304c\u3067\u304d\u307e\u3059\u306d\ud83d\udc4d<\/p>\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>\u307e\u305a\u300c<strong>http:\/\/******\/item\/create<\/strong>\u300d\u306b\u30a2\u30af\u30bb\u30b9\u3057\u307e\u3059\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-8202\" style=\"border: 3px solid #000;\" src=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2020\/10\/laravel_validation_error_structure_4.png\" alt=\"\" width=\"582\" height=\"353\" srcset=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2020\/10\/laravel_validation_error_structure_4.png 582w, https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2020\/10\/laravel_validation_error_structure_4-300x182.png 300w\" sizes=\"auto, (max-width: 582px) 100vw, 582px\" \/><\/p>\n<p>\u305d\u3057\u3066\u3001\u4eca\u56de\u306f\u30d0\u30ea\u30c7\u30fc\u30b7\u30e7\u30f3\u306e\u30c6\u30b9\u30c8\u3067\u3059\u306e\u3067\u3001\u305d\u306e\u307e\u307e\u306e\u72b6\u614b\u3067\u9001\u4fe1\u3057\u3066\u307f\u307e\u3059\u3002<\/p>\n<p>\u3067\u306f\u3001\u9001\u4fe1\u7d50\u679c\u3092\u958b\u767a\u30c4\u30fc\u30eb\u3067\u898b\u3066\u307f\u307e\u3057\u3087\u3046\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-8203\" style=\"border: 3px solid #000;\" src=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2020\/10\/laravel_validation_error_structure_5.png\" alt=\"\" width=\"507\" height=\"118\" srcset=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2020\/10\/laravel_validation_error_structure_5.png 507w, https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2020\/10\/laravel_validation_error_structure_5-300x70.png 300w\" sizes=\"auto, (max-width: 507px) 100vw, 507px\" \/><\/p>\n<p>\u306f\u3044\uff01<br \/>\n\u3046\u307e\u304f\u30ad\u30fc\u3054\u3068\u306b\u3072\u3068\u3064\u3060\u3051\u306e\u30a8\u30e9\u30fc\u3092\u53d6\u5f97\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3057\u305f\u3002<\/p>\n<p>\u6210\u529f\u3067\u3059\ud83d\ude0a\u2728<\/p>\n<h1>\u304a\u308f\u308a\u306b<\/h1>\n<p>\u3068\u3044\u3046\u3053\u3068\u3067\u3001\u4eca\u56de\u306f\u3044\u3064\u3082\u3068\u306f\u5c11\u3057\u9055\u3063\u305f\u5185\u5bb9\u306e\u8a18\u4e8b\u3092\u304a\u5c4a\u3051\u3057\u307e\u3057\u305f\u3002<\/p>\n<p>\u3061\u306a\u307f\u306b\u3001\u901a\u5e38\u8fd4\u3055\u308c\u308b\u914d\u5217\u306e\u5834\u5408\u306f<code>JavaScript<\/code>\u5185\u3067\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u3057\u3066\u683c\u7d0d\u3057\u76f4\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n<pre><strong>let errors = {};<\/strong>\r\n\r\n<strong>for(let key in error.response.data.errors) {<\/strong>\r\n\r\n<strong>    let responseError = error.response.data.errors[key];<\/strong>\r\n<strong>    errors[key] = responseError[0];<\/strong>\r\n\r\n<strong>}<\/strong>\r\n\r\n<strong>this.errors = errors;<\/strong><\/pre>\n<p>\u3082\u3057<code>Vue<\/code>\u306a\u3069\u3067\u4f7f\u3046\u5834\u5408\u306f\u3001\u4eca\u56de\u306e<code>Trait<\/code>\u306e\u3088\u3046\u306b<code>mixin<\/code>\u3092\u4f5c\u3063\u3066\u304a\u3044\u3066\u3001\u305d\u3053\u304b\u3089\u4f7f\u3044\u56de\u3057\u3067\u304d\u308b\u3088\u3046\u306b\u3057\u3066\u304a\u304f\u306e\u3082\u3044\u3044\u304b\u3082\u3057\u308c\u307e\u305b\u3093\u3002<\/p>\n<p>\u305c\u3072\u3044\u308d\u3044\u308d\u3068\u5de5\u592b\u3057\u3066\u3088\u308a\u697d\u306b\u958b\u767a\u3067\u304d\u308b\u3088\u3046\u306b\u3057\u3066\u3044\u3063\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-3467 aligncenter\" src=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2019\/04\/no_9.png\" alt=\"\" width=\"216\" height=\"320\" srcset=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2019\/04\/no_9.png 216w, https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2019\/04\/no_9-203x300.png 203w\" sizes=\"auto, (max-width: 216px) 100vw, 216px\" \/>\u300c\u30b3\u30fc\u30d2\u30fc\u3092\u6fc3\u304f\u3057\u3059\u304e\u305f\u7d50\u679c\u3001<br \/>\n\u80c3\u304c\u30ad\u30ea\u30ad\u30ea\u8a00\u3063\u3066\u307e\u3059\u30fb\u30fb\u30fb\ud83d\ude2d\u300d<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u3055\u3066\u3055\u3066\u3001Laravel\u304c\u3053\u308c\u307b\u3069\u4eba\u6c17\u306e\u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af\u306b\u306a\u3063\u305f\u306e\u306f\u3044\u308d\u3044\u308d\u3068\u7406\u7531\u304c\u3042\u308b\u3068\u601d\u3044\u307e\u3059\u304c\u3001\u500b\u4eba\u7684\u306b\u305d\u306e\u3072\u3068\u3064\u3068\u306b\u300c\u624b\u8efd\u306b\u30d0\u30ea\u30c7\u30fc\u30b7\u30e7\u30f3\uff08\u5165\u529b\u30c1\u30a7\u30c3\u30af\uff09 &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/blog.capilano-fw.com\/?p=8195\" class=\"more-link\"><span class=\"screen-reader-text\">&#8220;Laravel\u306e\u30d0\u30ea\u30c7\u30fc\u30b7\u30e7\u30f3\u30fb\u30a8\u30e9\u30fc\u30e1\u30c3\u30bb\u30fc\u30b8\u3092\u30b7\u30f3\u30d7\u30eb\u69cb\u6210\u306b\u3059\u308b&#8221; \u306e<\/span>\u7d9a\u304d\u3092\u8aad\u3080<\/a><\/p>\n","protected":false},"author":1,"featured_media":8207,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[5],"class_list":["post-8195","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-laravel","tag-5"],"_links":{"self":[{"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=\/wp\/v2\/posts\/8195","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=8195"}],"version-history":[{"count":5,"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=\/wp\/v2\/posts\/8195\/revisions"}],"predecessor-version":[{"id":8206,"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=\/wp\/v2\/posts\/8195\/revisions\/8206"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=\/wp\/v2\/media\/8207"}],"wp:attachment":[{"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=8195"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=8195"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=8195"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}