Laravel 6中的路由不支持POST方法

时间:2019-09-20 05:40:35

标签: laravel laravel-6

我正在Laravel 6中构建一个讨论表单。我使用的路由是POST方法,并在route:list中进行了检查。我收到以下错误,为什么?

  

此路由不支持POST方法。支持的方法:   GET,HEAD,PUT,PATCH,DELETE

查看

<form action="{{ route('replies.store', $discussion->slug) }}" method="post">
    @csrf
    <input type="hidden" name="contents" id="contents">
    <trix-editor input="contents"></trix-editor>
    <button type="submit" class="btn btn-success btn-sm my-2">
        Add Reply
    </button>
</form>

路线

Route::resource('discussions/{discussion}/replies', 'RepliesController');

控制器

public function store(CreateReplyRequest $request, Discussion $discussion)
{
    auth()->user()->replies()->create([
        'contents' => $request->contents,
        'discussion_id' => $discussion->id
    ]);

    session()->flash('success', 'Reply Added.');

    return redirect()->back();
}

1 个答案:

答案 0 :(得分:0)

您传递了一个讨论对象作为参数,以便将user_id存储在数组中。 我认为这不是存储数据的好习惯。

您可能会注意到route / web.php和html动作都很好,并使用了post,但是您收到了: “ Laravel 6中的路由不支持POST方法”。这是运行时错误。当您的逻辑对编译器没有意义时,可能会发生这种情况。

以下步骤可以帮助您完成所需的工作:

1。雄辩的模型(App \ Discussion)

protected $fillable = ['contents'];

public function user(){
   return $this->belongsTo('App\User');
}

2。雄辩的模型(App \ User)

public function discussions(){
   return $this->hasMany('App\Discussion');
}

3。控制器

use App\Discussion;

public function store(Request $request){
   //validate data
    $this->validate($request, [
        'contents' => 'required'
    ]);

   //get mass assignable data from the request
    $discussion = $request->all();

   //store discussion data using discussion object.
   Discussion::create($discussion);

   session()->flash('success', 'Reply Added.');

   return redirect()->back();
}

4。路线(routes / web.php)

Route::post('/replies/store', 'RepliesController@store')->name('replies.store');

5。查看

<form action="{{ route('replies.store') }}" method="post">
   @csrf
   <input type="hidden" name="contents" id="contents">
   <trix-editor input="contents"></trix-editor>
   <button type="submit" class="btn btn-success btn-sm my-2">
    Add Reply
   </button>
</form>