0

I am creating a basic crud to get familiarized with how Laravel functions, and I have the view, edit, delete, index working smooth however when I attempt to "create" I get the following error on submission.

methodnotallowedhttpexception

Any idea why I would be getting this? Please let me know if you need any additional code snips.

Here is my blade template for create.

<div class="panel panel-default">
      <div class="panel-heading">
        <h3 class="panel-title">Add a Lead</h3>
      </div>
      <div class="panel-body">
        <div class="table-container">
          <form method="POST" action="{{ url('leads/create') }}"  role="form">
            {{ csrf_field() }}
            <div class="row">
              <div class="col-xs-6 col-sm-6 col-md-6">
                <div class="form-group">
                  <input type="text" name="first_name" id="first_name" class="form-control input-sm" placeholder="First Name">
                </div>
              </div>
              <div class="col-xs-6 col-sm-6 col-md-6">
                <div class="form-group">
                  <input type="text" name="last_name" id="last_name" class="form-control input-sm" placeholder="Last Name">
                </div>
              </div>
              <div class="col-xs-6 col-sm-6 col-md-6">
                <div class="form-group">
                  <input type="text" name="primary_phone" id="primary_phone" class="form-control input-sm" placeholder="Primary Phone #">
                </div>
              </div>
              <div class="col-xs-6 col-sm-6 col-md-6">
                <div class="form-group">
                  <input type="text" name="source" id="source" class="form-control input-sm" placeholder="Lead Source">
                </div>
              </div>
            </div>
            <div class="row">
              <div class="col-xs-12 col-sm-12 col-md-12">
                <input type="submit"  value="Save" class="btn btn-success btn-block">
            </div>
          </form>
        </div>
      </div>
    </div>
  </div>

Here is my php artisan route:list

+--------+-----------+------------------------+------------------+------------------------------------------------------------------------+--------------+
| Domain | Method    | URI                    | Name             | Action                                                                 | Middleware   |
+--------+-----------+------------------------+------------------+------------------------------------------------------------------------+--------------+
|        | GET|HEAD  | /                      |                  | Closure                                                                | web          |
|        | GET|HEAD  | api/user               |                  | Closure                                                                | api,auth:api |
|        | GET|HEAD  | home                   | home             | App\Http\Controllers\HomeController@index                              | web,auth     |
|        | POST      | leads                  | leads.store      | App\Http\Controllers\LeadsController@store                             | web          |
|        | GET|HEAD  | leads                  | leads.index      | App\Http\Controllers\LeadsController@index                             | web          |
|        | GET|HEAD  | leads/create           |                  | Closure                                                                | web          |
|        | GET|HEAD  | leads/index            |                  | Closure                                                                | web          |
|        | PUT|PATCH | leads/{lead}           | leads.update     | App\Http\Controllers\LeadsController@update                            | web          |
|        | GET|HEAD  | leads/{lead}           | leads.show       | App\Http\Controllers\LeadsController@show                              | web          |
|        | DELETE    | leads/{lead}           | leads.destroy    | App\Http\Controllers\LeadsController@destroy                           | web          |
|        | GET|HEAD  | leads/{lead}/edit      | leads.edit       | App\Http\Controllers\LeadsController@edit                              | web          |
|        | POST      | login                  |                  | App\Http\Controllers\Auth\LoginController@login                        | web,guest    |
|        | GET|HEAD  | login                  | login            | App\Http\Controllers\Auth\LoginController@showLoginForm                | web,guest    |
|        | POST      | logout                 | logout           | App\Http\Controllers\Auth\LoginController@logout                       | web          |
|        | POST      | password/email         | password.email   | App\Http\Controllers\Auth\ForgotPasswordController@sendResetLinkEmail  | web,guest    |
|        | GET|HEAD  | password/reset         | password.request | App\Http\Controllers\Auth\ForgotPasswordController@showLinkRequestForm | web,guest    |
|        | POST      | password/reset         |                  | App\Http\Controllers\Auth\ResetPasswordController@reset                | web,guest    |
|        | GET|HEAD  | password/reset/{token} | password.reset   | App\Http\Controllers\Auth\ResetPasswordController@showResetForm        | web,guest    |
|        | GET|HEAD  | register               | register         | App\Http\Controllers\Auth\RegisterController@showRegistrationForm      | web,guest    |
|        | POST      | register               |                  | App\Http\Controllers\Auth\RegisterController@register                  | web,guest    |
|        | GET|HEAD  | splash                 |                  | Closure                                                                | web          |
|        | GET|HEAD  | users                  |                  | Closure                                                                | web          |
+--------+-----------+------------------------+------------------+------------------------------------------------------------------------+--------------+

Bonus Question: How do I add auth as middleware to my leads controller so that a user must be logged in to view it?

My routes defined in web.php are the following.

// Leads
Route::resource('leads','LeadsController');
Route::get('leads/index', function () { return view('leads.index'); });
Route::get('leads/create', function () { return view('leads.create'); });

2 Answers 2

2

Your form submits a post request, while your leads/create route is registered as a GET route. so either make your form method="get" or register your route as post i.e. Route::post('leads/create', function(){ /* do something here*/ });

Sign up to request clarification or add additional context in comments.

Comments

1

The method of your form is a POST request, however in your routes you only have a get request for the url 'leads/create'.

GET|HEAD | leads/create

Creating a POST route should fix it.

5 Comments

Can you elaborate on how I would add it as a POST route?
Updated op with routes for leads
What laravel version are you using? You should have a web.php file in your routes folder. There you can add routes. It should be pretty self explanatory when you get there.
I have 5.5, and I am familiar with web.php I posted in my OP the routes I have pertaining to the Leads Crud. I tried using Oasma's answer below but it just redirected the page to the index instead of posting the files. Route::post('leads/create', function(){ return view('leads.index'); });
Ok so if you make the route, you will have to direct it to somewhere where the post request can be dealth with. Route::post('leads/create', function(){ return view('leads.index'); }); Will lead to the view leads.index. Route::post('leads/create', 'PostController@index'); Will lead you to the index function in the post controller, where you can deal with the post request. The post data will be in request()->all(), or request('key')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.