3

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

Zak
  • 185
  • 2
  • 4
  • 12

2 Answers2

2

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;
Collin
  • 914
  • 1
  • 9
  • 30
  • Hi, thanks for you reply I have added "use Illuminate\Support\Facades\Validator;" to my EventsController but I am still getting the error '404 Not Found' when the 'addEvent' attempts to redirect to 'admin.events.index'.. I am a bit baffled as this route does exist? I appreciate your help. Thanks. – Zak Feb 21 '20 at 13:22
  • can you do php artisan route:list, Does the route show up? – Collin Feb 21 '20 at 13:40
  • Managed to fix the redirect issue, I trialled a few different methods of redirecting a route and this worked - return redirect()->route('admin.events.index'); ..as opposed to return Redirect::to('admin.events.index'); which did not work and returned 'not found'... i was unaware there was multiple ways to route .. anyway its all a learning curve.. thank you again for your help - much appreciated. – Zak Feb 21 '20 at 19:31
-1

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.

Nilay Shah
  • 16
  • 3
  • Hi, thank you for your response.. i made your change as suggested however another error appeared in my EventsController - 'validator class not found'.. i proceeded to add this and that is fine. I wonder if you would be able to help further.. the redirect i am returning within the validator is now appearing as '404 not found', I'm not sure what I'm doing wrong.. I am trying to redirect to the 'admin.events.index' when an event is added successfully.. I will paste my code below. I appreciate your help. – Zak Feb 21 '20 at 11:44
  • EventsController: 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); – Zak Feb 21 '20 at 11:44
  • @KerryMckinney Check my awnser, i will add a possible solution for the validator exception – Collin Feb 21 '20 at 13:07