2

I'm trying to create a new "Airborne" test in my program and getting a 405 MethodNotAllowed Exception.

Routes

Route::post('/testing/{id}/airbornes/create', [
    'uses' => 'AirborneController@create'
]);

Controller

public function create(Request $request, $id)
{
    $airborne = new Airborne;

    $newairborne = $airborne->newAirborne($request, $id);

    return redirect('/testing/' . $id . '/airbornes/' . $newairborne)->with(['id' => $id, 'airborneid' => $newairborne]);
}

View

<form class="sisform" role="form" method="POST" href="{{ URL::to('AirborneController@create', $id) }}">
    {{ csrf_field() }}
    {!! Form::token(); !!}
    <button type="submit" name="submit" value="submit" class="btn btn-success">
        <i class="fas fa-plus fa-sm"></i> Create
    </button>
</form>
Karl Hill
  • 12,937
  • 5
  • 58
  • 95
Curtis Thompson
  • 63
  • 1
  • 2
  • 10

3 Answers3

2

According to my knowledge forms don't have a href attribute. I think you suppose to write Action but wrote href. Please specify action attribute in the form that you are trying to submit.

<form method="<POST or GET>" action="<to which URL you want to submit the form>">

in your case its

<form method="POST" ></form>

And action attribute is missing. If action attribute is missing or set to ""(Empty String), the form submits to itself (the same URL).

For example, you have defined the route to display the form as

Route::get('/airbornes/show', [
    'uses' => 'AirborneController@show'
    'as' => 'airborne.show'
]);

and then you submit a form without action attribute. It will submit the form to same route on which it currently is and it will look for post method with the same route but you dont have a same route with a POST method. so you are getting MethodNotAllowed Exception.

Either define the same route with post method or explicitly specify your action attribute of HTML form tag.

Let's say you have a route defined as following to submit the form to

Route::post('/airbornes/create', [
        'uses' => 'AirborneController@create'
        'as' => 'airborne.create'
    ]);

So your form tag should be like

<form method="POST" action="{{ route('airborne.create') }}">
//your HTML here
</form>
Rafay Hassan
  • 740
  • 8
  • 23
1

MethodNotAllowedHttpException signposts that your route isn't available for the HTTP request method specified. Perhaps either because it isn’t defined correctly, or it has a conflict with another similarly named route.

Named Routes

Consider using named routes to allow for the convenient generation of URLs or redirects. They can generally be much easier to maintain.

Route::post('/airborne/create/testing/{id}', [
    'as' => 'airborne.create',
    'uses' => 'AirborneController@create'
]);

Laravel Collective

Use Laravel Collective's Form:open tag and remove Form::token()

{!! Form::open(['route' => ['airborne.create', $id], 'method' =>'post']) !!}

<button type="submit" name="submit" value="submit" class="btn btn-success">
    <i class="fas fa-plus fa-sm"></i> Create
</button>

{!! Form::close() !!}

dd() Helper Function

The dd function dumps the given variables and ends execution of the script. Double-check your Airborne class is returning the object or id you expect.

dd($newairborne)

List available routes

Always make sure your defined routes, views, and actions match up.

php artisan route:list --sort name
Karl Hill
  • 12,937
  • 5
  • 58
  • 95
0

First of All
Form don't have href attribute, it has "action"

<form class="sisform" role="form" method="POST" action="{{ URL::to('AirborneController@create', $id) }}">

Secondly
If the above change doesn't work, you can make some changes like:

1. Route
Give your route a name as:

Route::post('/testing/{id}/airbornes/create', [
    'uses' => 'AirborneController@create',
    'as'   => 'airborne.create',         // <---------------
]);

2. View
Give route name with route() method in form action rather than URL::to() method:

<form class="sisform" role="form" method="POST" action="{{ route('airborne.create', $id) }}">
TalESid
  • 2,304
  • 1
  • 20
  • 41