Jonas Hoffmann
Jonas Hoffmann

Reputation: 37

Laravel 5.1: MethodNotAllowedHttpException

When i make a request from a form in Laravel in order to store it in a database, it throws this error: MethodNotAllowedHttpException.

I cannot understand what i have done wrong so can you please help me?

Here is my form:

<form method="POST" action="/admin/ajax/edit">
        {{ csrf_field() }}
        <div class="textEdit">
            <div class="marginizer"> 
                <textarea id="edit" name="edit"></textarea>
            </div>
        </div>
        <input type="submit">
    </form>

Here is my routes file:

Route::get('admin/dashboard', 'Dashboard@index');
Route::get('admin/dashboard/{id}', 'Dashboard@show');
Route::get('admin/dashboard/edit/{site}', 'Edit@edit');
Route::get('admin', 'Dashboard@index');

// Register and Login routes...
Route::get('admin/login', 'Login@index');
Route::get('admin/register', 'Register@index');

// Authentication routes...
Route::get('auth/login', 'Auth\AuthController@getLogin');
Route::post('auth/login', 'Auth\AuthController@postLogin');
Route::get('auth/logout', 'Auth\AuthController@getLogout');

// Registration routes...
Route::get('auth/register', 'Auth\AuthController@getRegister');
Route::post('auth/register', 'Auth\AuthController@postRegister');

// Ajax routes
Route::post('admin/ajax/edit', 'EditAjax@store');

// UI routes
Route::get('/', 'ThemeLoader@index');
Route::get('/{site}', 'ThemeLoader@show');

Route::get('migrate', 'migrate@migrate');
Route::get('migrate/refresh', 'migrate@refresh');

And here is my controller which handles the request:

public function store(Request $request) {

    $content = new Content;

    $content->site = 'Index';
    $content->block = 1;
    $content->content = $request->input('edit');
    $content->active = 1;

    $content->save();
}

It actually stores the data in the database correctly but it continues to come up with an error.

Upvotes: 0

Views: 635

Answers (4)

keyur0517
keyur0517

Reputation: 95

Try this code, i guess it may help you:

<?php 
    {{ Form::open(array('action' => 'YourController@YourAction', 'method' => 'post')) }}
    {{ csrf_field() }}
        "..Your Form Inputs.."
    {{ Form::close() }}
?>

Upvotes: 0

Bagus Tesa
Bagus Tesa

Reputation: 1695

Well, have you check your route via php artisan route:list ? Sometimes when i fiddle with route, i got MethodNotAllowedHttpException and had to clear it's cache.

Edit as kofi pointed. it is possible if you had your laravel project directory not direct child of htdocs. So, it is better to try {{ route('admin/ajax/edit') }}

Upvotes: 1

Kiran Subedi
Kiran Subedi

Reputation: 2284

MethodNotAllowedHttpException
Usually happens when you try to use an HTTP method for a route but you have not defined that method in your routes file. For example, if you POST to a method, but you only define a GET method for the route in routes.php.

Upvotes: 0

kofi
kofi

Reputation: 381

Try this:

<form method="POST" action="{{ route('admin/ajax/edit') }}">

Upvotes: 0

Related Questions