Reputation: 21
I am building a discussion form in Laravel 6. The route I used is a POST method and I checked it in route:list
. I get the following error, why?
The POST method is not supported for this route. Supported methods: GET, HEAD, PUT, PATCH, DELETE
View
<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
Route::resource('discussions/{discussion}/replies', 'RepliesController');
Controller
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();
}
Upvotes: 1
Views: 2319
Reputation: 592
You passed a disccussion object as parameter in order to store user_id within an array. I think this is not a good practice to store data.
You might notice that your routes/web.php and your html action are fine and use post but you received: "POST method not supported for route in Laravel 6". This is runtime error. This probably happens when your logic does not make sense for the compiler.
The steps below might help you to accomplish what you want:
protected $fillable = ['contents'];
public function user(){
return $this->belongsTo('App\User');
}
public function discussions(){
return $this->hasMany('App\Discussion');
}
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();
}
Route::post('/replies/store', 'RepliesController@store')->name('replies.store');
<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>
Upvotes: 0