Laravel Route resource destroy not working - php

Here is my form :
<form action="{{ route('invoice.destroy' , $invoice->id)}}" method="DELETE">
<div class="modal-footer no-border">
<button type="button" class="btn btn-info" data-dismiss="modal">No</button>
<button type="submit" class="btn btn-primary">Yes</button>
<input type="hidden" name="_method" value="DELETE" />
</div>
</form>
Here is my controller :
public function destroy($id)
{
$invoice = Invoice::find($id);
if(!$invoice){
return redirect()->route('invoice.index')->with(['fail' => 'Page not found !']);
}
$invoice->delete();
return redirect()->route('invoice.index')->with(['success' => 'Invoice Deleted.']);
}
But it can not delete where is the problem ? How solve this ?

You need to use POST method for the form and add input element with name _method and value DELETE. Also, add token:
<form action="{{ route('invoice.destroy' , $invoice->id)}}" method="POST">
<input name="_method" type="hidden" value="DELETE">
{{ csrf_field() }}
<div class="modal-footer no-border">
<button type="button" class="btn btn-info" data-dismiss="modal">No</button>
<button type="submit" class="btn btn-primary">Yes</button>
</div>
</form>

I think you must add a hidden input to the form which will contain the method used:
<form action="{{ route('invoice.destroy' , $invoice->id)}}" method="POST">
<input type="hidden" name="_method" value="DELETE" />
</form>
Read more on Laravel documentation about Form method spoofing

In order to get PUT and DELETE methods to work, you need an additional field, since only POST and GET are possible within HTML (out-of-the-box).
The additional field will be made with the code:
{!! method_field('DELETE') !!}
So your form will look like this:
<form action="{{ route('invoice.destroy' , $invoice->id)}}" method="DELETE">
{!! method_field('DELETE') !!}
<div class="modal-footer no-border">
<button type="button" class="btn btn-info" data-dismiss="modal">No</button>
<button type="submit" class="btn btn-primary">Yes</button>
</div>
</form>

Also, if you are using blade templates, you can add the method field like so:
#method('DELETE')

More Laravel way you can do this
<form action="{{ route('invoice.destroy',$invoice->id)}}" method="POST">
#method('DELETE')
<button type="submit" class="btn btn-primary">Yes</button>
</form>

Related

Laravel CRUD - Delete - Not route found

Error:
Route [events.destroy] not defined. (View: C:\xampp\htdocs\Event-Manager-Project\resources\views\events\admin.blade.php)
In my view, admin i have this about the delete:
<form action="{{ route('events.destroy', $event->id) }}" method="POST">
#csrf
#method('DELETE')
<button type="submit" class="far fa-trash-alt" ></button>
</form>
In controller this:
public function destroy($id)
{
$event = Event::find($id);
$event->delete();
return redirect('/admin');
}
And using a route resource:
Route::resource('admin', 'App\Http\Controllers\EventController');
change the code to this
<form action="{{ route('events.destroy', $event->id) }}" method="post">
<input type="hidden" name="_method" value="DELETE" />
<input type="submit" value="Delete" name="Delete" id="btnExc" class="btn btn-sm btn-danger glyphicon glyphicon-trash" accesskey="x"/>
</form>

The DELETE method is not supported for this route. Supported methods: GET, HEAD, POST. in laravel 7'

I am trying to use Resource Controller and having a problem with destroying method, can't find a solution.
I get this error
The DELETE method is not supported for this route. Supported methods: GET, HEAD, POST.
web.php
Route::resource('honor', 'HonorController');
HonorController.php
public function destroy(Honor $honor)
{
dd($honor);
$honor->delete();
return redirect()->back();
}
blade
<form action="{{ route('honor.destroy', $honor->id) }}" method="post">
#csrf
#method('DELETE')
<div class="btn-group">
Edit
<button type="submit" class="btn btn-danger btn-sm">Delete</button>
</div>
</form>
Try changing the key (uses honor, not id) to match the name of the parameter in the route.
For example:
<form action="{{ route('honor.destroy', ['honor' => $honor->id]) }}" method="post">
#csrf
#method('DELETE')
<div class="btn-group">
Edit
<button type="submit" class="btn btn-danger btn-sm">Delete</button>
</div>
</form>

The GET method is not supported for this route. Supported methods: DELETE

I got this error when I try to remove subcategories by however I did use the delete method.
blade.php:-
<form action="{{ url('sub_category/delete',$item->id) }}" method="DELETE">
<button type="submit" class="btn btn-outline-danger">remove category</button>
</form>
my web
Route::DELETE('/sub_category/delete/{id}','SubcategoryController#destroy');
controller:-
public function destroy($id)
{
$subcategory= Subcategory::where('id',$id)->delete();
return redirect()->route('cars.index');
// ->with('success','Car deleted successfully');
}
Your form should be in the following way
<form action="{{ url('sub_category/delete',$item->id) }}" method="POST">
<input name="_method" type="hidden" value="DELETE">
#csrf
<button type="submit" class="btn btn-outline-danger">remove category</button>
</form>
DELETE,PUT,PATCH,HEAD methods should be defined as follows
#method('DELETE') #method('PUT') ...
Laravel routing
<form action="{{ url('sub_category/delete',$item->id) }}" method="POST">
#method('DELETE')
#csrf
<button type="submit" class="btn btn-outline-danger">remove category</button>
</form>
try like this one:
<form action="{{ url('sub_category/delete',$item->id) }}" method="POST">
{{method_field('DELETE')}}
#csrf
<button type="submit" class="btn btn-outline-danger">remove category</button>
</form>
your route:
Route::delete('/sub_category/delete/{id}','SubcategoryController#destroy');

What could be the issue in my route that gets me MethodNotAllowedHttpException for my PUT, PATCH, DELETE and others routes

Can you please help me with my issue. I am trying my best to fix the issue, but so far I have failed.
I even upgraded to Laravel 7.x with my application and still It kept on getting the same issue over and over again. Could my issue be in the user interface (UI)?
below are the errors that i'm getting:
Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException
The DELETE method is not supported for this route. Supported methods: GET, HEAD, POST.
Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException
The PUT method is not supported for this route. Supported methods: GET, HEAD, POST.
Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException
The PATCH method is not supported for this route. Supported methods: GET, HEAD, POST.
This is my route
Route::get('/', function () {
//return redirect(route('login'));
return redirect()->route('login');
});
Auth::routes();
Route::middleware(['auth']) -> group(function() {
Route::get('/home', 'HomeController#index')->name('home');
Route::resource('/device', 'DeviceController');
Route::get('/laptops', 'LaptopsController#index')->name('laptops');
});
This is my form edit
<form action="/device" method="POST" id="editForm">
{{csrf_field()}}
{{ method_field('PUT') }}
<div class="col-md-4 mb-3">
<label>Serial Number</label>
<input type="text" name="Serial_No" id="" class="form-control" placeholder="Enter Serial number">
</div>
<button class="btn btn-primary" type="submit">Add Data</button>
<button type="reset" class="btn btn-default float-right">Cancel</button>
<button type="reset" class="btn btn-default float-middle">Clear</button>
</form>
This is my delete form
<form action="/device" method="POST" id="deleteForm">
{{csrf_field()}}
{{method_field('DELETE')}}
<div class="form-row">
<input type="hidden" name="_method" value="DELETE" >
<P>Are You Sure!.. You want to delete this Device?</P>
</div>
<button class="btn btn-primary " type="submit" >YES! DELETE DEVICE</button>
<button type="button" class="btn btn-secondary float-right" data-dismiss="modal" >CANCEL</button>
</form>

The DELETE & PUT method is not supported for this route. Supported methods: GET, HEAD,POST

I need your help guys on this, my app can't delete neither can it Update. it keeps on popping these errors on my edit and Delete Buttons;
(1/1) MethodNotAllowedHttpException
The DELETE method is not supported for this route. Supported methods: GET, HEAD.
(1/1) MethodNotAllowedHttpException
The PUT method is not supported for this route. Supported methods: GET, HEAD.
this is my route
Route::group(['middleware' => ['role:Admin']], function () {
Route::resource('/device', 'DeviceController');
});
this is my edit blade
<form action="/device" method="POST" id="editForm">
{{csrf_field()}}
{{ method_field('PUT') }}
<div class="col-md-4 mb-3">
<label>Serial Number</label>
<input type="text" name="Serial_No" id="" class="form-control" placeholder="Enter Serial number">
</div>
<button class="btn btn-primary" type="submit">Add Data</button>
<button type="reset" class="btn btn-default float-right">Cancel</button>
<button type="reset" class="btn btn-default float-middle">Clear</button>
</form>
This is my delete blade
<form action="/device" method="POST" id="deleteForm">
{{csrf_field()}}
{{method_field('DELETE')}}
<div class="form-row">
<input type="hidden" name="_method" value="DELETE" >
<P>Are You Sure!.. You want to delete this Device?</P>
</div>
<button class="btn btn-primary " type="submit" >YES! DELETE DEVICE</button>
<button type="button" class="btn btn-secondary float-right" data-dismiss="modal" >CANCEL</button>
</form>
This is my controller for delete
public function destroy($id)
{
$devices = Device::find($id);
$devices -> delete();
return Redirect::back() -> with('success','Data Deleted Successfully');
}
This is my script that deletes
<script>
//Start Delete Record
table.on('click', '.delete', function () {
$tr = $(this).closest('tr');
if ($($tr).hasClass('child')) {
$tr = $tr.prev('.parent');
}
var data = table.row($tr).data();
console.log(data);
$('#deleteForm').attr('action', '/laptops/'+data[0]);
$('#deleteModal').modal('show');
});
//End Delete Record
});
</script>
You are not passing the correct actions
your update form should look like this instead
<form action="" method="POST" id="editForm">
{{csrf_field()}}
{{ method_field('PUT') }}
<div class="col-md-4 mb-3">
<label>Serial Number</label>
<input type="text" name="Serial_No" id="" class="form-control" placeholder="Enter Serial number">
</div>
<button class="btn btn-primary" type="submit">Add Data</button>
<button type="reset" class="btn btn-default float-right">Cancel</button>
<button type="reset" class="btn btn-default float-middle">Clear</button>
</form>
your delete form should look like this instead.
<form action="" method="POST" id="deleteForm">
{{csrf_field()}}
{{method_field('DELETE')}}
<div class="form-row">
<p>Are You Sure!.. You want to delete this Device?</p>
</div>
<button class="btn btn-primary " type="submit">YES! DELETE DEVICE</button>
<button type="button" class="btn btn-secondary float-right" data-dismiss="modal">
CANCEL
</button>
</form>
your script should look like this (change 'laptops/' to 'device/')
table.on('click', '.delete', function () {
$tr = $(this).closest('tr');
if ($($tr).hasClass('child')) {
$tr = $tr.prev('.parent');
}
var data = table.row($tr).data();
console.log(data);
$('#deleteForm').attr('action', '/device/'+data[0]);
$('#deleteModal').modal('show');
});
Also as an opinion you could pluralize your resource route instead of the singular approach. i.e /devices instead of /device
so in your web.php routes file you have:
Route::group(['middleware' => ['role:Admin']], function () {
Route::resource('/devices', 'DeviceController');
});
That way you have a more friendly route list like:
GET - /devices
GET - /devices/create
POST - /devices/update
GET - /devices/{device}
GET - /devices/{device}/edit
PUT/PATCH - /devices/{device}
DELETE - /devices/{device}
All these can be viewed in your cli using php artisan route:list

Categories