こちらの本の説明に沿い、載っている本管理アプリを制作していた際に発生したエラーについての質問です。
テキストエリアに文字(本のタイトル)を入力し、送信(post)を行うとMethodNotAllowedHttpExceptionが発生します。
以下に各コード
php
1web.php 2 3use App\Book; 4use Illuminate\Http\Request; 5 6/*ほんのダッシュボード表示*/ 7Route::get('/', function () { 8 $books = Book::orderBy('created_at', 'asc')->get(); 9 return view('books',[ 10 'books' => $books 11 ]); 12}); 13 14/*新「本」を追加*/ 15Route::get('/books', function (Request $request) { 16 //バリデーション 17 $validator = Validator::make($request->all(), [ 18 'item_name' => 'required|max:255', 19 ]); 20 //バリデーション: エラー 21 if ($validator->fails()) { 22 return redirect('/') 23 ->withInput() 24 ->withErrors($validator); 25 } 26 // Eloquent モデル 27 $books = new Book; 28 $books->item_name = $request->item_name; 29 $books->item_number = '1'; 30 $books->item_amount = '1000'; 31 $books->published = '2017-03-07 00:00:00'; 32 $books->save(); // 「/」 ルートにリダイレクト 33 return redirect('/'); 34}); 35 36/*本を削除*/ 37Route::get('/book/{book}', function (Book $book) { 38 $book->delete(); 39 return redirect('/'); 40});
php
1books.blade.php 2 3@extends('layouts.app') 4 5@section('content') 6 7<!-- Bootstrap の 定形 コード… --> 8 9<div class="panel-body"> 10 <!-- バリデーションエラーの 表示 に 使用 --> 11 @include('common.errors') 12 <!-- バリデーションエラーの 表示 に 使用 --> 13 14 <!-- 本 登 録 フォーム --> 15 <form action="{{ url('books')}}" method="POST" class="form-horizontal"> 16 {{csrf_field()}} 17 18 <!-- 本 のタイトル --> 19 <div class="form-group"> 20 <label for="book" class="col-sm-3 control-label">Book</label> 21 <div class="col-sm-6"> 22 <input type="text" name="item_name" id="book-name" class="form-control"> 23 </div> 24 </div> 25 26 <!-- 本 登 録 ボタン --> 27 <div class="form-group"> 28 <div class="col-sm-offset-3 col-sm-6"> 29 <button type="submit" class="btn btn-default"> 30 <i class="glyphicon glyphicon-plus"></i> Save </button> 31 </div> 32 </div> 33 </form> 34 @if (count($books) > 0) 35 <div class="panel panel-default"> 36 <div class="panel-heading"> 37 現 在 䛾 本 38 </div> 39 <div class="panel-body"> 40 <table class="table table-striped task-table"> 41 <!-- テーブルヘッダ --> 42 <thead> 43 <th> 本 一 覧 </th> 44 <th> </th> 45 </thead> 46 <!-- テーブル 本体 --> 47 <tbody> 48 @foreach ($books as $book) 49 <tr> 50 <!-- 本 タイトル --> 51 <td class="table-text"> 52 <div>{{ $book->item_name }}</div> 53 </td> 54 55 <!-- 本: 削除 ボタン --> 56 <td> 57 <form action="{{ url('book/'.$book->id)}}" method="POST"> 58 {{csrf_field()}} 59 {{method_field('DELETE')}} 60 61 <button type="submit" class="btn btn-danger"> 62 <i class="glyphicon glyphicon-trash"></i> 削除 63 </button> 64 </form> 65 </td> 66 </tr> 67 @endforeach 68 </tbody> 69 </table> 70 </div> 71 </div> 72 @endif 73</div> 74 75<!-- Book: 既 に 登 録 されてる 本 のリスト --> 76@endsection 77コード
ご指摘のほどよろしくお願いします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/06/12 05:20