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

質問編集履歴

1

細く

2018/07/11 02:37

投稿

masateru2
masateru2

スコア39

title CHANGED
File without changes
body CHANGED
@@ -154,4 +154,42 @@
154
154
  $('#body').val('');
155
155
  });
156
156
 
157
+ ```
158
+ addPostはこちらで設定しています。
159
+ ```php
160
+ <?php
161
+
162
+ namespace App\Http\Controllers;
163
+
164
+ use Illuminate\Http\Request;
165
+ use App\Post;
166
+ use Validator;
167
+ use RSesponse;
168
+ use Illuminate\Support\Facedes\input;
169
+ use App\http\Requests;
170
+ class PostController extends Controller
171
+ {
172
+ public function index(){
173
+ $post = Post::all();
174
+ return view('post.index',compact('post'));
175
+ }
176
+
177
+ public function addPost(Request $req){
178
+ $rules = array(
179
+ 'title' => 'required',
180
+ 'body' =>'required',
181
+ );
182
+ $validator = Varidator::make( input::all(),$rules);
183
+ if($validator->fails()){
184
+ return response::json(array('errors'=>$validator->getMessage()->toarray()));
185
+ }else{
186
+ $post = new post;
187
+ $post->title = $req->title;
188
+ $post->body = $req->body;
189
+ $post->save();
190
+ return response()->json($post);
191
+ }
192
+ }
193
+ }
194
+
157
195
  ```