Laravel - The POST method is not supported for this route. Supported methods: GET, HEAD

Viewed 11994

I am trying to add an event on a calendar I have created, however I am getting the following error

The POST method is not supported for this route. Supported methods: GET, HEAD

I have used the methods @csrf and {{ method_field('PUT') }} to no avail. I have also cleared route cache which did not help. Any help is much appreciated.

Routes:

Route::get('/', function () {
    return view('welcome');
});

Auth::routes();

Route::get('/home', 'HomeController@index')->name('home');

Route::namespace('Admin')->prefix('admin')->name('admin.')->group(function(){
    Route::middleware('can:manage-users')->group(function(){
        Route::resource('/users', 'UsersController', ['except' => ['show']]);
        Route::resource('/courses', 'CoursesController', ['except' => ['show']]);
    });
    Route::middleware('can:manage-calendar')->group(function(){
        Route::get('events', 'EventsController@index')->name('events.index');
        Route::post('/addEvents', 'EventsController@addEvent')->name('events.add');
    });
})

index.blade.php

@extends('layouts.app')
@section ('content')
<div class="container">
    <div class="row justify-content-center">
        <div class="col-md-14">
                <div class="card">
                        <div class="card-header">Calendar</div>
                        <div class="card-body">
                        {!! Form::open(array('route' => 'admin.events.index', 'method' => 'POST', 'files' => 'true'))!!}
                            {{-- {{method_field('PUT') }}  
                             @csrf  --}}
                        <div class="row">
                            <div class="col-xs-12 col-sm-12 col-md-12"></div>
                            <div class="col-xs-4 col-sm-4 col-md-4">
                            <div class="form-group">
                            {!! Form::label('event_name', 'Event Name:') !!}
                                <div class="">
                                    {!! Form::text('event_name', null, ['class' => 'form-control']) !!}
                                    {!! $errors->first('event_name', '<p class="alert alert-danger">:message</p>') !!}
                                </div>

@Collin, I have added the image below in relation to your question

enter image description here

2 Answers

The error actually explains the problem. The method POST is not supported for the route you're using. You are trying to post to the route: admin.events.index but you actually want to post to the route events.add.

Route::post('/addEvents', 'EventsController@addEvent')->name('events.add');

  {!! Form::open(array('route' => 'admin.events.add', 'method' => 'POST', 'files' => 'true'))!!}

                            {{-- @csrf  --}}

Adding to this awnser is a possible solution for the validator exception the OP has mentioned in the comments.

The validator not found error can possibly come from the following:

When adding the the following code:

    public function addEvent(Request $request) 
{ 
$validator = Validator::make($request->all(), 
[ 'event_name' => 'required', 
'start_date' => 'required', 
'end_date' => 'required' ]); 

if ($validator->fails()) 
{ \Session::flash('warning', 'Please enter the valid details'); return Redirect::to('admin.events.index')->withInput()->withErrors($validator);

Try adding:

use Illuminate\Support\Facades\Validator;

Just check your form action url route. You have to pass 'route('admin.events.add)' rather than 'route('admin.events.index')' and also dont use 'PUT' it will accept 'POST' as well.

Related