LoginSignup
4
6

More than 3 years have passed since last update.

Laravel エラー 備忘録

Posted at

Laravelで簡単なアプリケーションを作成しているときに、詰まってしまった箇所を備忘録として記事にしようと思います。

ルーティング


Route::get('contents/{id}/update', 'ContentsController@edit');
Route::post('/book/{book}', 'ContentsController@update');

コントローラー

    public function edit($id)
    {
        $book = Book::find($id);
        return view('contents/update')->with('book',$book);
    }

    ~中略~

    public function update(Request $request, $id)
    {
        $book = Book::find($id);
        $book->title = $request->title;
        $book->body = $request->body;
        $book->todo = $request->todo;
        $book->limit = $request->limit;
        $book->save();
        return redirect('/');
    }

ビュー


       <form action="/contents/update" method="POST" class="form-horizontal">
        
       <div class="form-group">
              <label for="task-name" class="col-sm-3 control-label" >感想または学び</label>

              <div class="col-sm-6">
                <textarea type="text" name="body"  class=" big-box" value="{{ old('book') }}">{{ $book->body }}
                </textarea>
              </div>
            </div>

問題

この状態でhttp://127.0.0.1:8000/contents/updateへアクセスするとThe POST method is not supported for this route. Supported methods: GET, HEAD, PUT, PATCH, DELETE.というエラーが表示される。

解決法

ビューファイルを修正する

      <!-- /contents/updateを/book/{{ $book->id }}に修正する -->
          <form action="/book/{{ $book->id }}" method="POST" class="form-horizontal">
            {{ csrf_field() }}

中略~

             <div class="col-sm-6">
                <!-- value="{{ old('book') }}"を削除する -->
                <textarea type="text" name="body"  class=" big-box" >{{ $book->body }}
                </textarea>
              </div>

actionの送信先を間違えていたのと、value="{{ old('book') }}"を記述していたことが原因でした。

4
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
6