質問編集履歴

1

細く

2018/07/11 02:37

投稿

masateru2
masateru2

スコア39

test CHANGED
File without changes
test CHANGED
@@ -311,3 +311,79 @@
311
311
 
312
312
 
313
313
  ```
314
+
315
+ addPostはこちらで設定しています。
316
+
317
+ ```php
318
+
319
+ <?php
320
+
321
+
322
+
323
+ namespace App\Http\Controllers;
324
+
325
+
326
+
327
+ use Illuminate\Http\Request;
328
+
329
+ use App\Post;
330
+
331
+ use Validator;
332
+
333
+ use RSesponse;
334
+
335
+ use Illuminate\Support\Facedes\input;
336
+
337
+ use App\http\Requests;
338
+
339
+ class PostController extends Controller
340
+
341
+ {
342
+
343
+ public function index(){
344
+
345
+ $post = Post::all();
346
+
347
+ return view('post.index',compact('post'));
348
+
349
+ }
350
+
351
+
352
+
353
+ public function addPost(Request $req){
354
+
355
+ $rules = array(
356
+
357
+ 'title' => 'required',
358
+
359
+ 'body' =>'required',
360
+
361
+ );
362
+
363
+ $validator = Varidator::make( input::all(),$rules);
364
+
365
+ if($validator->fails()){
366
+
367
+ return response::json(array('errors'=>$validator->getMessage()->toarray()));
368
+
369
+ }else{
370
+
371
+ $post = new post;
372
+
373
+ $post->title = $req->title;
374
+
375
+ $post->body = $req->body;
376
+
377
+ $post->save();
378
+
379
+ return response()->json($post);
380
+
381
+ }
382
+
383
+ }
384
+
385
+ }
386
+
387
+
388
+
389
+ ```