1

I am getting an MethodNotAllowedHttpException error while im trying to update mine post. So I googled the error and found this laravel throwing MethodNotAllowedHttpException but it get explained that i need to make the route an post request, where mine form actions go thruw but its already a post and it keeps throwing the same error and i cant figure out if the erros is in the form the web.php or the controller it self

edit.blade.php

    <form method="POST" action="/posts/{{ $post->id }}/edit">

        {{ csrf_field() }}
        @method('PUT')

        <div class="form-group">
            <label for="title">Title:</label>
            <input type="text" class="form-control" id="title" name="title" value="{{ $post->title }}">
        </div>

        <div class="form-group">
            <label for="body">Body:</label>
            <textarea id="body" name="body" class="form-control" rows="10">
                {{
                $post->body
                }}
            </textarea>
        </div>

        <div class="form-group">

            <button type="submit" class="btn btn-primary">Edit</button>

        </div>

        @include('layouts.errors')

    </form>

Web.php

Route::get('/', 'PostsController@index')->name('home');
Route::get('/posts/create', 'PostsController@create');
Route::post('/posts', 'PostsController@store');
Route::get('/posts/{post}', 'PostsController@show');
Route::get('/posts/{post}/edit', 'PostsController@edit');
Route::post('/posts/{post}/edit', 'PostsController@update');
Route::get('/posts/{post}/delete', 'PostsController@destroy');

PostsController.php (this is the part that matters out of the controller if u want me to post the hole controller let me know)

public function edit(Post $post)

{

    return view('posts.edit', compact('post'));

}

public function update(Request $request, Post $post)

{

    $request->validate([

        'title' => 'required',

        'body' => 'required'
    ]);

    $post->update($request->all());

    return redirect('/')->with('succes','Product updated succesfully');

}
  • 1
    There is no route defined for the `PUT` method, you are overriding the form's `POST` with a `PUT` via `@method('PUT')`. Either change the existing definition or add:`Route::put('/posts/{post}/edit', 'PostsController@update');` – ka_lin Nov 12 '18 at 13:25
  • i changed it in the web.php and it works thank you – InterstingJavaLearner Nov 12 '18 at 13:30

4 Answers4

3

You should try this:

View file:

<form method="POST" action="{{ route('post.update',[$post->id]) }}">

        {{ csrf_field() }}


        <div class="form-group">
            <label for="title">Title:</label>
            <input type="text" class="form-control" id="title" name="title" value="{{ $post->title }}">
        </div>

        <div class="form-group">
            <label for="body">Body:</label>
            <textarea id="body" name="body" class="form-control" rows="10">
                {{
                $post->body
                }}
            </textarea>
        </div>

        <div class="form-group">

            <button type="submit" class="btn btn-primary">Edit</button>

        </div>

        @include('layouts.errors')

    </form>

Your Route

Route::post('/posts/{post}/edit', 'PostsController@update')->name('post.update');
0

You are submitting the form with the put method as defining @method('PUT') will make it a put route. Either define a route for put method like this Route::put('/posts/{post}/edit', 'PostsController@update'); or remove @method('PUT') from your blade file.

Kamal Paliwal
  • 1,251
  • 8
  • 16
0

This problem 1 week took me but i solve it with routing

In edit.blade.php

{!! Form::open([route('post.update',[$post->id]),'method'=>'put']) !!}
 <div class="form-group">
        <label for="title">Title:</label>
        {!! Form::text('title',$post->title,['class'=>'form-control',
        'id'=>'title']) !!}
 </div>
 <div class="form-group">
        <label for="body">Body:</label>
        {!! Form::textarea('body', $post->body, ['id' => 'body', 'rows' => 10, 
        class => 'form-control']) !!}
 </div>
 <div class="form-group">
    {!! Form::submit('Edit',['class'=>'btn btn-block bg-primary',
    'name'=>'update-post']) !!}
 </div>
{!! Form::close() !!}
@include('layouts.errors')

In Web.php

Route::resource('posts','PostsController');
Route::get('/posts/{post}/delete', 'PostsController@destroy');
Route::put('/posts/{post}/edit',['as'=>'post.update',
           'uses'=>'PostController@update']);
Route::get('/', 'PostsController@index')->name('home');
-1

Laravel will throw a MethodNotAllowedHttpException exception also if a form is placed by mistake this way inside a table:

<table><form><tr><td>...</td></tr></form><table>

instead of this :

<table><tr><td><form>...</form></td></tr></table>
Grigoreas P.
  • 2,452
  • 25
  • 19