ScottieJoe's avatar

MethodNotAllowedHTTPException when trying to create new data entry.

I have a form for creating a new entry into my database: http://puu.sh/p8KTD/00868d0d79.png

This is what my store() function in my controller looks like: http://puu.sh/p8KWe/779909e18e.png

Whenever I try and enter in stuff into the form and it update, I get a MethodNotAllowedHttpException: http://puu.sh/p8KYn/71e20582b6.png

I have no idea why I'm getting this and especially not how to fix it.

P.S. Sorry for using image links to show my code, I don't know how to format on here.

0 likes
13 replies
zachleigh's avatar

What does your routes file look like?

To post code, wrap it in three back ticks.

```

// code

```

ScottieJoe's avatar

Here you go:

<?php

/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| Here is where you can register all of the routes for an application.
| It's a breeze. Simply tell Laravel the URIs it should respond to
| and give it the Closure to execute when that URI is requested.
|
*/

Route::get('/', function () {
    $posts = Post::all();

    return View::make('home')->withPosts($posts);
});

Route::resource('post', 'PostController');

zachleigh's avatar

What does the view look like? Are you making a post request?

ScottieJoe's avatar

Yes I am, here's the line:

<a href="{{URL::route('post.create')}}" class="btn btn-primary btn-lg btn-block" role="button">
        <span class="glyphicon glyphicon-comment"></span> New Post </a>
shafiqkr's avatar

@ScottieJoe

the issue is with your route...you did it as Route::get while you need post. so use post instead of get.

ScottieJoe's avatar

I don't think that is the problem. The error is thrown when I try and click "Submit" in my form. Here's the code:

    {{ Form::model(array('method' => 'POST', 'route' => array('post.create')))}}
    {{ Form::label('title','Post Title: ', array('class' => 'pull-left'))  }}
    {{ Form::text('title', null, ['class' => 'form-control'])}}
    <br>
    {{ Form::label('message','Post Message: ', array('class' => 'pull-left'))  }}
    {{ Form::textarea('message', null, ['class' => 'form-control'])}}
    <br>
    <a href="/" class="btn btn-danger pull-left" role="button">Cancel</a>
    {{ Form::submit('Submit',  ['class' => 'btn btn-success pull-right']) }}
    {{ Form::close() }}

My store() function:

    public function store()
    {
        $post = new Post();
        $input = Input::all();

        $post->authour_id = '1';
        $post->title = $input['title'];
        $post->message = $input['message'];
        $post->comment_count = '0';
        $post->save();

        $posts = Post::all();

        return View::make('home', compact('posts'));
zachleigh's avatar

Your controller method is called store, but you are calling create. Change the name of the controller method to 'create.'

ScottieJoe's avatar

I changed the route in the Form::model line:

    {{ Form::model(array('method' => 'PUT', 'route' => array('post.store')))}}
    {{ Form::label('title','Post Title: ', array('class' => 'pull-left'))  }}
    {{ Form::text('title', null, ['class' => 'form-control'])}}
    <br>
    {{ Form::label('message','Post Message: ', array('class' => 'pull-left'))  }}
    {{ Form::textarea('message', null, ['class' => 'form-control'])}}
    <br>
    <a href="/" class="btn btn-danger pull-left" role="button">Cancel</a>
    {{ Form::submit('Submit',  ['class' => 'btn btn-success pull-right']) }}
    {{ Form::close() }}

Still doesn't work.

zachleigh's avatar

Ah, theres your problem. There is not actually a PUT method. Change it to post and put this in your form:

{{ method_field('PUT') }}
ScottieJoe's avatar

Like this?

    {{ Form::model(array('method' => 'POST', 'route' => array('post.store')))}}
    {{ Form::label('title','Post Title: ', array('class' => 'pull-left'))  }}
    {{ Form::text('title', null, ['class' => 'form-control'])}}
    <br>
    {{ Form::label('message','Post Message: ', array('class' => 'pull-left'))  }}
    {{ Form::textarea('message', null, ['class' => 'form-control'])}}
    <br>
    <a href="/" class="btn btn-danger pull-left" role="button">Cancel</a>
    {{ Form::submit('Submit',  ['class' => 'btn btn-success pull-right']) }}
    {{ method_field('PUT') }}
    {{ Form::close() }}

That brings up an error.

zachleigh's avatar

Older version of Laravel? Try this:

<input type="hidden" name="_method" value="PUT">

Please or to participate in this conversation.