【问题标题】:Laravel 5.7 MethodNotAllowedHttpExceptionLaravel 5.7 MethodNotAllowedHttpException
【发布时间】:2019-04-15 05:32:41
【问题描述】:

我在尝试更新我的帖子时收到 MethodNotAllowedHttpException 错误。所以我用谷歌搜索了这个错误并找到了这个laravel throwing MethodNotAllowedHttpException,但它得到解释我需要制作路线 一个发布请求,我的表单操作通过了,但它已经是一个帖子,它一直抛出相同的错误,我无法确定错误是 web.php 还是它自己的控制器

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 (如果你想让我发布孔控制器,这是控制器之外重要的部分,让我知道)

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');

}

【问题讨论】:

  • 没有为PUT 方法定义路由,您通过@method('PUT')PUT 覆盖表单的POST。更改现有定义或添加:Route::put('/posts/{post}/edit', 'PostsController@update');
  • 我在 web.php 中修改了它,谢谢

标签: php laravel laravel-5.7


【解决方案1】:

如果表单以这种方式错误地放置在表格中,Laravel 也会抛出 MethodNotAllowedHttpException 异常:

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

而不是这个:

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

【讨论】:

    【解决方案2】:

    这个问题花了我 1 周的时间,但我用路由解决了它

    在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')
    

    在 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');
    

    【讨论】:

      【解决方案3】:

      你应该试试这个:

      查看文件

      <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>
      

      您的路线

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

      【讨论】:

        【解决方案4】:

        您正在使用 put 方法提交表单,因为定义 @method('PUT') 将使其成为 put 路由。要么为 Route::put('/posts/{post}/edit', 'PostsController@update'); 这样的 put 方法定义一个路由,要么从你的刀片文件中删除 @method('PUT')

        【讨论】:

          猜你喜欢
          • 2019-07-01
          • 1970-01-01
          • 1970-01-01
          • 2013-07-04
          • 2015-09-11
          • 2017-12-21
          • 2018-02-28
          • 2016-08-31
          • 2018-01-13
          相关资源
          最近更新 更多