Laravel-MethodNotAllowedHttpException。发布

时间:2018-10-22 12:34:18

标签: php laravel

我收到了 MethodNotAllowedHttpException 。我的路线已定义,并显示在 route:list 中。我正在尝试创建一条记录。

这是路线

Route::post('add-counsellor/{id}', 'SupportGroupsController@addCounsellor')->name('add-additional-counsellor');

其前缀为/ admin / support-groups。

我的表单是

<form action"/admin/support-groups/add-counsellor/{{$data->id}}" method="post">
    {{ csrf_field() }}
    <div class="flex-grid__section">
        <label for="counsellor" class="flex-2-col control-label">{{ trans('Add additional Facilitator') }}</label>
        <div class="flex-4-col" id="counsellor_row1">
            <select class="form-control" name="counsellor">
                <option disabled>Select a Facilitator</option>
                @foreach ($counsellor as $key => $couns)
                <option value="{{$key}}">{{$couns}}</option>
                @endforeach
            </select>
        </div>
    </div>

    <input type="submit" value="Add counsellor">
</form>

我的控制器是

public function addCounsellor($id){
      $participant = Participant::create(
          [
              'thread_id'     => $id,
              'user_id'       => request($counsellor),
              'last_read'     => new Carbon()
          ]
      );

      return back();
    }

任何想法。

2 个答案:

答案 0 :(得分:1)

您的表单标签中有错误,不仅应该action=,而且action也应该使用url()函数,而不是发送完整的网址

<form action="{{ url('admin/support-groups/add-counsellor/'.$data->id) }}" method="post">

答案 1 :(得分:0)

您在这里遇到错误<form action"/admin/support-groups/add-counsellor/{{$data->id}}" method="post">,您错过了 = <form action="/admin/support-groups/add-counsellor/{{$data->id}}" method="post">

是的,当您通过laravel内置了urlroute之类的函数时,请利用它们而不是键入完整的URL。

使用route

<form action"{{ route('add-additional-counsellor',$data->id) }}" method="post">
    {{ csrf_field() }}
    <div class="flex-grid__section">
        <label for="counsellor" class="flex-2-col control-label">{{ trans('Add additional Facilitator') }}</label>
        <div class="flex-4-col" id="counsellor_row1">
            <select class="form-control" name="counsellor">
                <option disabled>Select a Facilitator</option>
                @foreach ($counsellor as $key => $couns)
                <option value="{{$key}}">{{$couns}}</option>
                @endforeach
            </select>
        </div>
    </div>

    <input type="submit" value="Add counsellor">
</form>

使用url

<form action="{{ url('admin/support-groups/add-counsellor/'.$data->id) }}" method="post">
        {{ csrf_field() }}
        <div class="flex-grid__section">
            <label for="counsellor" class="flex-2-col control-label">{{ trans('Add additional Facilitator') }}</label>
            <div class="flex-4-col" id="counsellor_row1">
                <select class="form-control" name="counsellor">
                    <option disabled>Select a Facilitator</option>
                    @foreach ($counsellor as $key => $couns)
                    <option value="{{$key}}">{{$couns}}</option>
                    @endforeach
                </select>
            </div>
        </div>

        <input type="submit" value="Add counsellor">
    </form>

route函数中,您只需要提供使用->name('route.blah');定义的路由的名称,然后传递该路由的参数,而在url方法中,您可以传递完整的路由然后您可以使用串联附加参数。 我更喜欢route方法,因为它很简单。