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
I don't see a POST method in any of your delete or update routes.
When you click button in your form, it does a HTTP post. So change your routes to
Route:post(...)to match accordingly.