質問編集履歴

3

説明追記

2019/08/13 13:50

投稿

hasshy
hasshy

スコア102

test CHANGED
File without changes
test CHANGED
@@ -58,7 +58,7 @@
58
58
 
59
59
  ```php
60
60
 
61
- Route::post('/import', 'School\UserController@csvImport')
61
+ Route::post('/import', 'UserController@csvImport')
62
62
 
63
63
  ->name('csv.import');
64
64
 
@@ -66,8 +66,126 @@
66
66
 
67
67
 
68
68
 
69
+ ### UserController
70
+
71
+ ルートで呼び出すコントローラーと
72
+
73
+ ```php
74
+
75
+ public function csvImport(UserCsvImportRequest $request){
76
+
77
+ dd($request);
78
+
79
+ }
80
+
81
+ ```
82
+
83
+
84
+
85
+ ### UserCsvImportRequest
86
+
87
+ コントローラーで呼び出すRequestの処理は下記です。
88
+
89
+
90
+
91
+ ```php
92
+
93
+ <?php
94
+
95
+
96
+
97
+ namespace App\Http\Requests;
98
+
99
+
100
+
101
+ use Illuminate\Foundation\Http\FormRequest;
102
+
103
+
104
+
105
+ class UserCsvImportRequest extends FormRequest
106
+
107
+ {
108
+
109
+ /**
110
+
111
+ * Determine if the user is authorized to make this request.
112
+
113
+ *
114
+
115
+ * @return bool
116
+
117
+ */
118
+
119
+ public function authorize()
120
+
121
+ {
122
+
123
+ return true;
124
+
125
+ }
126
+
127
+
128
+
129
+ public function attributes()
130
+
131
+ {
132
+
133
+ return [
134
+
135
+ 'import_file' => 'インポートするファイル'
136
+
137
+ ];
138
+
139
+ }
140
+
141
+
142
+
143
+ /**
144
+
145
+ * Get the validation rules that apply to the request.
146
+
147
+ *
148
+
149
+ * @return array
150
+
151
+ */
152
+
153
+ public function rules()
154
+
155
+ {
156
+
157
+ return [
158
+
159
+ 'import_file' => [
160
+
161
+ 'required',
162
+
163
+ 'max:5',
164
+
165
+ 'file',
166
+
167
+ 'mimes:csv'
168
+
169
+ ]
170
+
171
+ ];
172
+
173
+ }
174
+
175
+ }
176
+
177
+
178
+
179
+ ```
180
+
181
+
182
+
183
+
184
+
69
185
  ## 試した事
70
186
 
187
+ ### メソッドを変える
188
+
71
189
  試しに、メソッドをgetに変えてみました。
72
190
 
73
191
 
@@ -85,3 +203,39 @@
85
203
  変更してGetでアクセスすると、下記のエラーが吐かれており、POSTでもGETでも、HEADでもない状態なのがわかりました。
86
204
 
87
205
  > 405 The POST method is not supported for this route. Supported methods: GET, HEAD.
206
+
207
+
208
+
209
+
210
+
211
+ #### GET,POSTを指定する
212
+
213
+ 両方のメソッドを指定すると、Postが実行できることを確認できました。
214
+
215
+
216
+
217
+ ```php
218
+
219
+ Route::match(['get', 'post'], '/import', 'School\UserController@csvImport')
220
+
221
+ ->name('csv.import');
222
+
223
+ ```
224
+
225
+
226
+
227
+ ### Requestをデフォルトに変える
228
+
229
+ 自作したRequestクラスに変えたところ、post出来ました。
230
+
231
+
232
+
233
+ ```php
234
+
235
+ public function csvImport(Request $request){
236
+
237
+ dd($request);
238
+
239
+ }
240
+
241
+ ```

2

form修正

2019/08/13 13:50

投稿

hasshy
hasshy

スコア102

test CHANGED
File without changes
test CHANGED
@@ -30,7 +30,7 @@
30
30
 
31
31
  ```php
32
32
 
33
- <form method="POST" action="{{ route('csv.import') }}" accept-charset="UTF-8">
33
+ <form method="POST" action="{{ route('csv.import') }}" accept-charset="UTF-8" enctype="multipart/form-data" >
34
34
 
35
35
  @csrf
36
36
 

1

誤記

2019/08/13 13:20

投稿

hasshy
hasshy

スコア102

test CHANGED
@@ -1 +1 @@
1
- 【Laravel】FormでPostしてもGetとして送信されてしまう。
1
+ 【Laravel】FormでPostしてもPostとして送信できない
test CHANGED
File without changes