简体   繁体   中英

Getting MethodNotAllowedHttpException in RouteCollection.php line 219: on laravel

Im getting the errors above. I tried to read on other forums with the same problem but with no luck. My create, store and edit are working. However when updating my form im getting the error above. Can someone help me on this. Thanks

{!! Form::model($enrollment['method'=>'POST','route'=>['/enrollment',$enrollment->id],'class'=>'form-horizontal']) !!}

                <div class="form-group">
                    <label for="subject_code" class="col-md-3 control-label">Subject Code</label>
                    <div class="col-md-8">
                        <select class="form-control" name="_method" value="PUT" id="subject_code">
                            <option value="{{ $enrollment->subject_code }}">{{ $enrollment->subject_code }}</option>
                            @foreach($subjects as $subject)
                                <option value="{{ $subject->subject_code }}">{{ $subject->subject_code}}</option>
                            @endforeach 
                        </select>
                    </div>

                </div>

                <div class="form-group">
                    <label for="subject_description" class="col-md-3 control-label">Subject description</label>
                    <div class="col-md-8">
                        <select class="form-control" name="subject_description" id="subject_description">
                            <option value="{{ $enrollment->subject_description }}">{{ $enrollment->subject_description }}</option>
                            @foreach($subjects as $subject)
                                <option value="{{ $subject->subject_description }}">{{ $subject->subject_description}}</option>
                            @endforeach 
                        </select>
                    </div>

                </div>


                <div class="form-group">
                    <label for="subject_code" class="col-md-3 control-label">Subject Code</label>
                    <div class="col-md-8">
                        <select class="form-control" name="subject_code" id="subject_code">
                            <option value="{{ $enrollment->section }}">{{ $enrollment->section}}</option>
                            @foreach($sections as $section)
                                <option value="{{ $section }}">{{ $section }}</option>
                            @endforeach
                        </select>
                    </div>

                </div>


                <div class="form-group">
                    <label for="subject_code" class="col-md-3 control-label">Subject Code</label>
                    <div class="col-md-8">
                        <select class="form-control" name="subject_code" id="subject_code">
                            <option value="{{ $enrollment->schedule }}">{{ $enrollment->schedule }}</option>
                            @foreach($subjects as $subject)
                                <option value="{{ $subject->schedule }}">{{ $subject->schedule}}</option>
                            @endforeach 
                        </select>
                    </div>

                </div>  

                <div class="form-group">
                    <label for="subject_code" class="col-md-3 control-label">Subject Code</label>
                    <div class="col-md-8">
                        <select class="form-control" name="subject_code" id="subject_code">
                            <option value="{{ $enrollment->no_of_units }}">{{ $enrollment->no_of_units }}</option>
                            @foreach($subjects as $subject)
                                <option value="{{ $subject->no_of_units }}">{{ $subject->no_of_units}}</option>
                            @endforeach 
                        </select>
                    </div>

                </div>
                <div class="form-group">
                    <div class="col-md-7 col-md-offset-3">

                        <button type="submit" class="btn btn-success">
                            <i class="fa fa-save"></i>
                            &nbsp;Save Changes
                        </button>

                        <button type="submit" class="btn btn-danger">
                            <i class="fa fa-times-circle"></i>
                            &nbsp;Delete
                        </button>
                    </div>
                </div>                                                                              
{!! Form::close() !!}

Here's my EnrollmentController:

    public function update(EnrollmentRequest $request, $id)
{
    $enrollment = Enrollment::findOrFail($id);
    $enrollment->update($request->all());
    return redirect('/enrollment');
}

routes.php

// 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');

Route::resource('enrollment','EnrollmentController');

I think the issue is that you have a mistake in your HTML. In your very first "subject_code" input, you have the following HTML:

<select class="form-control" name="_method" value="PUT" id="subject_code">

You've accidentally named this input as _method , which is the reserved input name for Laravel method spoofing. Even though you've assigned the value as "PUT", this is not how selects work, and the value will end up being something else. This is preventing the Laravel method spoofing from working correctly. Since the method spoofing is not working, you're sending a POST request to enrollment/{id} , and that route does not allow POST requests.

You need to correct this input so that it is not named "_method":

<select class="form-control" name="subject_code" id="subject_code">

Additionally, you need to fix your Form::model() statement. It needs to take two parameters, where the first is the model, and the second is an array of attributes. In your array of attributes, you need the "method" to be "PUT" (so that the form builder will automatically create the hidden "_method" input), and you need the first element in the "route" array to be the name of the route, not the url. Updates shown below:

{!! Form::model($enrollment, ['method' => 'PUT', 'route' => ['enrollment.update', $enrollment->id], 'class' => 'form-horizontal']) !!}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM