質問編集履歴
2
ソースの追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -94,6 +94,8 @@
|
|
94
94
|
|
95
95
|
}
|
96
96
|
|
97
|
+
|
98
|
+
|
97
99
|
ビュー
|
98
100
|
|
99
101
|
<form action="{{route('posts.store')}}" method="post">
|
@@ -116,7 +118,11 @@
|
|
116
118
|
|
117
119
|
</form>
|
118
120
|
|
121
|
+
|
122
|
+
|
119
|
-
|
123
|
+
ルーティング
|
124
|
+
|
125
|
+
Route::post('/posts','PostsController@store')->name('posts.store');
|
120
126
|
|
121
127
|
|
122
128
|
|
1
ソースの追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -28,13 +28,93 @@
|
|
28
28
|
|
29
29
|
```ここに言語名を入力
|
30
30
|
|
31
|
-
|
31
|
+
コントローラ
|
32
32
|
|
33
|
-
|
33
|
+
<?php
|
34
34
|
|
35
|
+
|
36
|
+
|
37
|
+
namespace App\Http\Controllers;
|
38
|
+
|
39
|
+
|
40
|
+
|
41
|
+
use Illuminate\Http\Request;
|
42
|
+
|
43
|
+
use App\Post;
|
44
|
+
|
45
|
+
use Auth;
|
46
|
+
|
47
|
+
|
48
|
+
|
49
|
+
class PostsController extends Controller
|
50
|
+
|
51
|
+
{
|
52
|
+
|
53
|
+
public function store(Request $request){
|
54
|
+
|
55
|
+
|
56
|
+
|
57
|
+
$request->validate([
|
58
|
+
|
59
|
+
'description'=>'required',
|
60
|
+
|
35
|
-
'image'=>'required|image|mimes:jpg,jpeg,png|max:2000',
|
61
|
+
'image'=>'required|image|mimes:jpg,jpeg,png|max:2000',
|
36
62
|
|
37
63
|
]);
|
64
|
+
|
65
|
+
|
66
|
+
|
67
|
+
$post=new Post;
|
68
|
+
|
69
|
+
$post->description=$request->description;
|
70
|
+
|
71
|
+
$post->user_id=Auth::user()->id;
|
72
|
+
|
73
|
+
|
74
|
+
|
75
|
+
if($request->hasFile('image')){
|
76
|
+
|
77
|
+
$file=$request->file('image')->store('images','public');
|
78
|
+
|
79
|
+
|
80
|
+
|
81
|
+
$post->image=$file;
|
82
|
+
|
83
|
+
}
|
84
|
+
|
85
|
+
|
86
|
+
|
87
|
+
|
88
|
+
|
89
|
+
$post->save();
|
90
|
+
|
91
|
+
return back();
|
92
|
+
|
93
|
+
}
|
94
|
+
|
95
|
+
}
|
96
|
+
|
97
|
+
ビュー
|
98
|
+
|
99
|
+
<form action="{{route('posts.store')}}" method="post">
|
100
|
+
|
101
|
+
{{csrf_field()}}
|
102
|
+
|
103
|
+
<div class="form-group">
|
104
|
+
|
105
|
+
<input type="file" name="image" class="form-control">
|
106
|
+
|
107
|
+
</div>
|
108
|
+
|
109
|
+
<div class="form-group">
|
110
|
+
|
111
|
+
<textarea name="description" class="form-control" placeholder="コメント..."></textarea>
|
112
|
+
|
113
|
+
</div>
|
114
|
+
|
115
|
+
<button type="submit" class="btn btn-info btn-block">保存</button>
|
116
|
+
|
117
|
+
</form>
|
38
118
|
|
39
119
|
```
|
40
120
|
|