teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

3

説明追記

2019/08/13 13:50

投稿

hasshy
hasshy

スコア102

title CHANGED
File without changes
body CHANGED
@@ -28,11 +28,70 @@
28
28
  管理画面配下のページなので、MiddlewareにAuthの処理が入っていますが、それ以外は入っていません。
29
29
 
30
30
  ```php
31
- Route::post('/import', 'School\UserController@csvImport')
31
+ Route::post('/import', 'UserController@csvImport')
32
32
  ->name('csv.import');
33
33
  ```
34
34
 
35
+ ### UserController
36
+ ルートで呼び出すコントローラーと
37
+ ```php
38
+ public function csvImport(UserCsvImportRequest $request){
39
+ dd($request);
40
+ }
41
+ ```
42
+
43
+ ### UserCsvImportRequest
44
+ コントローラーで呼び出すRequestの処理は下記です。
45
+
46
+ ```php
47
+ <?php
48
+
49
+ namespace App\Http\Requests;
50
+
51
+ use Illuminate\Foundation\Http\FormRequest;
52
+
53
+ class UserCsvImportRequest extends FormRequest
54
+ {
55
+ /**
56
+ * Determine if the user is authorized to make this request.
57
+ *
58
+ * @return bool
59
+ */
60
+ public function authorize()
61
+ {
62
+ return true;
63
+ }
64
+
65
+ public function attributes()
66
+ {
67
+ return [
68
+ 'import_file' => 'インポートするファイル'
69
+ ];
70
+ }
71
+
72
+ /**
73
+ * Get the validation rules that apply to the request.
74
+ *
75
+ * @return array
76
+ */
77
+ public function rules()
78
+ {
79
+ return [
80
+ 'import_file' => [
81
+ 'required',
82
+ 'max:5',
83
+ 'file',
84
+ 'mimes:csv'
85
+ ]
86
+ ];
87
+ }
88
+ }
89
+
90
+ ```
91
+
92
+
35
93
  ## 試した事
94
+ ### メソッドを変える
36
95
  試しに、メソッドをgetに変えてみました。
37
96
 
38
97
  ```php
@@ -41,4 +100,22 @@
41
100
  ```
42
101
 
43
102
  変更してGetでアクセスすると、下記のエラーが吐かれており、POSTでもGETでも、HEADでもない状態なのがわかりました。
44
- > 405 The POST method is not supported for this route. Supported methods: GET, HEAD.
103
+ > 405 The POST method is not supported for this route. Supported methods: GET, HEAD.
104
+
105
+
106
+ #### GET,POSTを指定する
107
+ 両方のメソッドを指定すると、Postが実行できることを確認できました。
108
+
109
+ ```php
110
+ Route::match(['get', 'post'], '/import', 'School\UserController@csvImport')
111
+ ->name('csv.import');
112
+ ```
113
+
114
+ ### Requestをデフォルトに変える
115
+ 自作したRequestクラスに変えたところ、post出来ました。
116
+
117
+ ```php
118
+ public function csvImport(Request $request){
119
+ dd($request);
120
+ }
121
+ ```

2

form修正

2019/08/13 13:50

投稿

hasshy
hasshy

スコア102

title CHANGED
File without changes
body CHANGED
@@ -14,7 +14,7 @@
14
14
  methodもPOSTなのでPostで処理をされているはずです。
15
15
 
16
16
  ```php
17
- <form method="POST" action="{{ route('csv.import') }}" accept-charset="UTF-8">
17
+ <form method="POST" action="{{ route('csv.import') }}" accept-charset="UTF-8" enctype="multipart/form-data" >
18
18
  @csrf
19
19
  <input class="cutom-file-input" id="csv-input" type="file" name="import_file"/>
20
20
  <div class="btn-group">

1

誤記

2019/08/13 13:20

投稿

hasshy
hasshy

スコア102

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