user3579562
user3579562

Reputation:

Laravel 4 throwing MethodNotAllowedHttpException

I am making a simple registration form that then submits data to my database. However, the problem that I am running into is that the code that should submit the info to the database is not working.

Here's the form that I made using Twitter Bootstrap

<!DOCTYPE html>
<html>
<body>
    <div class="modal-body">
        <div class="well">
            <div id="myTabContent" class="tab-content">
                <div class="tab-pane active in" id="login">
                    <form method="POST" action='/adding_to_table' class="form-horizontal">
                        <fieldset>
                            <div id="legend">
                                <legend class="">Create Your Account</legend>
                            </div>
                            <div class="control-group">
                                <!-- Username -->
                                <label class="control-label"  for="firstname">First Name</label>
                                <div class="controls">
                                    <input type="text" id="first_name" name="firstname" placeholder="" class="input-xlarge">
                                </div>
                            </div>

                            <div class="control-group">
                                <!-- Username -->
                                <label class="control-label"  for="lastname">Last Name</label>
                                <div class="controls">
                                    <input type="text" id="last_name" name="lastname" placeholder="" class="input-xlarge">
                                </div>
                            </div>

                            <div class="control-group">
                                <!-- Username -->
                                <label class="control-label"  for="email">E-mail</label>
                                <div class="controls">
                                    <input type="text" id="e_mail" name="email" placeholder="" class="input-xlarge">
                                </div>
                            </div>

                            <div class="control-group">
                                <!-- Username -->
                                <label class="control-label"  for="username">Username</label>
                                <div class="controls">
                                    <input type="text" id="username" name="username" placeholder="" class="input-xlarge">
                                </div>
                            </div>


                            <div class="control-group">
                                <!-- Password-->
                                <label class="control-label" for="password">Password</label>
                                <div class="controls">
                                    <input type="password" id="password" name="password" placeholder="" class="input-xlarge">
                                </div>
                            </div>

                            <div class="control-group">
                                <!-- Button -->
                                <div class="controls">
                                    <button class="btn btn-primary">Submit</button>
                                </div>
                            </div>
                        </fieldset>
                    </form>                
                </div>
            </div>
        </div>
    </div>

<script class="cssdeck" src="//cdnjs.cloudflare.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script class="cssdeck" src="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/2.3.1/js/bootstrap.min.js"></script>
</body>
<html>

This is the route that I am passing to the action field in my form:

  Route::get('/adding_to_table', function()
  {
      return View::make('create');
  });

And this is the create.php file that the route (above) is supposed to load which takes care of the database submission

  DB::table('user_info')->insert(
    array("First_name" => Input::post('firstname'),
          "Last_name" => Input::post("lastname"),
          "E-mail" => Input::post("email"),
          "Username" => Input::post("username"),
          "Password" => Input::post("password"),
        )
    );

echo "Successfully entered user information into data table";

However, Laravel doesn't like this and is throwing this error at me: Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException Open: /Users/brendanbusey/Desktop/php_site/laravelSite/bootstrap/compiled.php

        }))->bind($request);
        } else {
        $this->methodNotAllowed($others);
       }
   }
  protected function methodNotAllowed(array $others)
  {
      throw new MethodNotAllowedHttpException($others);
  }
  protected function check(array $routes, $request, $includingMethod = true)

I have double and triple checked to make sure that I am using the names and not the id's from my html form when I'm sending the data via POST and all my names from the columns in my database match the ones in my code. Any help would be greatly appreciated!

Upvotes: 1

Views: 17134

Answers (5)

Venkateswarlu Dande
Venkateswarlu Dande

Reputation: 1

in Create.blade.php

{!! Form::open(array('route' => 'ControllerName.store','method' => 'POST','files' => true)) !!}

in Controller :-

public function store(Request $request)
    {   
        $this->validate($request, [
            'first_name' =>'required',
            'last_name' => 'required',
            'e_mail' =>'required',
            'username' => 'required',
            'password' =>'required',
        ]);
ModelName::create($request->all());
return route->(ControllerName.index);

NOTE : check model with all fields are fillable are not .

Upvotes: -1

Ankit kumar
Ankit kumar

Reputation: 49

Just add {{ csrf_field() }} below your form, like this:

<form method="POST" action='/adding_to_table' class="form-horizontal">
    {{ csrf_field() }}

Upvotes: -1

Marios Fakiolas
Marios Fakiolas

Reputation: 1545

Change

Input::post() to Input::get()

to retrieve inputs.

Upvotes: 0

Don&#39;t Panic
Don&#39;t Panic

Reputation: 41820

You need to have two routes defined, one for GET and one for POST. If you want to use the same adding_to_table url, it should be something like

Route::get('/adding_to_table', function() {
    // code to display your form initially
});
Route::post('/adding_to_table', function() {
    // code to process the form submission
});

Maybe I'm not understanding you correctly, but it looks like you are using View::make() to try to run your db insert code. I believe View::make() is just intended to render blade templates, so I don't think this will work. Instead you could put that type of thing into a controller method, or even directly into the post route closure. To access the submitted values, you should use Input::get('firstname') etc. In the laravel docs here it explains that

You do not need to worry about the HTTP verb used for the request, as input is accessed in the same way for all verbs.

Upvotes: 7

lowerends
lowerends

Reputation: 5267

You need to change the form opening line in your HTML to this:

<form method="POST" action='adding_to_table' class="form-horizontal">

In your routes.php file, you need to have this:

Route::get('adding_to_table', function()
{
    return View::make('create');
});

Route::post('adding_to_table', function()
{
    DB::table('user_info')->insert(
        array("First_name" => Input::get('firstname'),
              "Last_name" => Input::get("lastname"),
              "E-mail" => Input::get("email"),
              "Username" => Input::get("username"),
              "Password" => Input::get("password"),
        )
    );
});

Upvotes: 0

Related Questions