Update and delete -- I can't update and delete data from database in Laravel framework

677 Views Asked by At

I am using a form to submit my request to update and delete my data from database but I am unable to do so.

I have tried using different methods but I could not solve this problem. I am using Laravel 5.4.36 for this project.

Controller:

public function update(Request $request, $id)
{
    Student::where('id', $id)->update(['first_name' => 'vks', 'last_name' => 'pok']);
}

public function destroy(Request $id)
{
    $student=Student::find($id);
    $student->delete();
}

Routes:

Route::get('/student/about', 'BksController@about');
Route::get('/student/service', 'BksController@Services');
Route::get('create','BksController@create');
Route::post('create','StudentController@create');
Route::get('search','BksController@search');
Route::post('search','StudentController@show');
Route::get('/update','BksController@update');
Route::put('update','StudentController@update');
Route::get('/delete','BksController@delete');
Route::delete('/delete','StudentController@destroy');
Route::resource('student', 'StudentController');
Route::get('/home', 'HomeController@index')->name('home');

Blade Template for the destroy form:

@include('student.commonlayout')

<div class='col-md-6 col-md-offset-3'>
  <h1>DeleteData</h1>
  <hr>
  <form method="post" action="{{url('delete')}}">
    {{csrf_field()}}
    <div class="form-group">
      <input type="text" name="first_name" class="form-control" placeholder="Enter your Id" />
    </div>
    <div class="form-group">
      <input type="submit" class="btn btn-primary"/>
    </div>
  </form>
</div>

Blade template for the update form:

@include('student.commonlayout')

<div class='col-md-6 col-md-offset-3'>
  <h1>update Data</h1>
  <hr>
  <form method="post" action="{{url('update')}}">
    {{csrf_field()}}
    <div class="form-group">
      <input type="text" name="id" class="form-control" placeholder="Enter your id" />
    </div>
    <div class="form-group">
      <input type="text" name="first_name" class="form-control" placeholder="Enter your Name" />
    </div>
    <div class="form-group">
      <input type="text" name="last_name" class="form-control" placeholder="Enter last Name" />
    </div>
    <div class="form-group">
      <input type="submit" class="btn btn-primary"  />
    </div>
  </form>
</div>

When updating, I receive the following error:

MethodNotAllowedHttpException in RouteCollection.php line 251

When deleting, I receive a similar error:

MethodNotAllowedHttpException in RouteCollection.php line 251

2

There are 2 best solutions below

1
na-98 On

I don't see a POST method in any of your delete or update routes.

  Route::get('/update','BksController@update');
  Route::put('update','StudentController@update');
  Route::get('/delete','BksController@delete');
  Route::delete('/delete','StudentController@destroy');

When you click button in your form, it does a HTTP post. So change your routes to Route:post(...) to match accordingly.

2
Namoshek On

Using Route::resource(...) will create the routes with HTTP request methods DELETE for the destroy action and PUT for the update action. This means you have to use these methods when submitting the form.

Unfortunately, HTML forms do not support PUT or DELETE. This is why Laravel uses a special hidden input called _method which is supposed to contain the correct method. The form itself is supposed to be sent via POST. This is what your update form should look like (fields and other markup ommitted):

<form method="post" action="{{ url('update') }}">
  @method('put')
  {{ csrf_field() }}

  <!-- Your fields here -->
</form>

The directive @method('put') will generate the following html <input type="hidden" name="_method" value="put">.

You can read more about this topic in the official documentation.