-2

When I Click On Edit Button It Will Display a Pop up box, when i edit the field and click on update, It display following error.

 Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException
No message

<div id="myEditModal" class="modal fade in" tabindex="-1" role="dialog" aria-labelledby="myModalLabelEdit" aria-hidden="true">
 <div class="modal-dialog">
                        <div class="modal-content">
                            <div class="modal-header">
                                <h4 class="modal-title" id="myModalLabelEdit">Edit Department</h4>
                                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                            </div>

                            <div class="modal-body">
                                <form class="form-horizontal" method="POST" action="{{ route('edit_department', $departmentList->id) }}">
                                @csrf
                                    <div class="form-group">
                                        <div class="col-md-12">
                                             <input type="text" name="nameOfDepartment" class="form-control" placeholder="Edit Department" value="{{$departmentList->nameOfDepartment}}">
                                        </div>
                                    </div>
                                </form>
                            </div>

                            <div class="modal-footer">
                                <button type="submit" class="btn btn-info waves-effect" data-dismiss="modal">Update</button>
                                <button type="button" class="btn btn-default waves-effect" data-dismiss="modal">Cancel</button>
                            </div>

                        </div>
                    </div>
                </div>

This Is my code of blade file of edit and following code is in my web.php file

Route::get('add-department', 'DepartmentController@createDepartment')->name('create_department');
Route::post('store-department', 'DepartmentController@storeDepartment')->name('store_department');
Route::get('list-department', 'DepartmentController@listDepartment')->name('list_department');
Route::get('edit-department/{id}', 'DepartmentController@editDepartment')->name('edit_department');
Route::post('update-department/{id}', 'DepartmentController@updateDepartment')->name('update_department');
Route::get('delete-department/{id}', 'DepartmentController@deleteDepartment')->name('delete_department');
John_rees
  • 93
  • 1
  • 3
  • 12
  • so many answers to the question already, one here: https://stackoverflow.com/questions/46865073/methodnotallowedhttpexception-while-update-record-in-laravel – nakov Apr 05 '19 at 07:28
  • You don't have a route that corresponds to the request method you are using. – Jerodev Apr 05 '19 at 07:28
  • Possible duplicate of [Laravel Exception 405 MethodNotAllowed](https://stackoverflow.com/questions/53674329/laravel-exception-405-methodnotallowed) – Harun Yilmaz Apr 05 '19 at 07:33

1 Answers1

0

You are already editing your record, now you need to update it in the database. So your route should be update:

First, correct your update route to: (from post to put)

Route::put('update-department/{id}', 'DepartmentController@updateDepartment')->name('update_department');

Then update your form accordingly:

<form class="form-horizontal" method="POST" action="{{ route('update_department', $departmentList->id) }}">
    @csrf
    @method('PUT')

    ...

What went wrong: Your form action was going to an Edit route which is using a GET method, but you used a POST method. This is why you got a MethodNotAllowedHttpException.

emotality
  • 12,795
  • 4
  • 39
  • 60