Laravel CRUD - Delete - Not route found - php

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>

Related

Laravel 6 404 Not found but route exists

Getting 404 Not found though route exists, the following codes worked perfectly on Laravel 8 but on 6 produces 404.
Route:
// Content Packs
Route::delete('content-packs/destroy', 'ContentPacksController#massDestroy')->name('content-packs.massDestroy');
Route::patch('content-packs/{content-pack}/clone_pack', 'ContentPacksController#clone_pack')->name('content-packs.clone_pack');
Route::resource('content-packs', 'ContentPacksController');
Button:
<form action="{{ route('admin.content-packs.clone_pack', $contentPack->id) }}" method="POST" onsubmit="return confirm('{{ trans('cruds.contentPack.clone_confirmation') }}');" style="display: inline-block;">
<input type="hidden" name="_method" value="PATCH">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<input type="submit" class="btn btn-xs btn-warning" value="{{ trans('cruds.contentPack.clone') }}">
</form>
Controller method:
public function clone_pack(Request $request, ContentPack $contentPack)
{
$contentPack = ContentPack::where('id', $request->id)->first();
$newPack = $contentPack->replicate();
$newPack->created_at = Carbon::now();
$newPack->save();
return back();
}
What am I missing?
Changing route to this fixed the issue:
Route::get('content-packs/content-pack/{id}', 'ContentPacksController#clone_pack')->name('content-packs.clone_pack');

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

post route in Laravel

I have done everything the right way but my submit button doesnt do anything and I dont know why....
Here is my view
<form action="{{ route('importUser') }}" method="POST" enctype="multipart/form-data">
#csrf
add users via excell<input name="file" class="form-control" style="padding-bottom:3em; margin-bottom:3em" type="file">
<div style="display:inline;">
<input type="submit" class="btn btn-primary btn-lg" value="ارفع" >
</div>
</form>
Here is my controller
function importUser(Request $request)
{
#code...
}
and my route
Route::POST('ImportUsersFile', 'ExcelUserController#importUser')->name('importUser')->middleware('Admin');
Apparently, the flow dont get in the function import user. I tried to dd into it but nothing happend!
According to an error message you provided in a comment, try this:
php artisan key:generate
Try using url intead of route
<form action="{{ url('ImportUsersFile') }}" method="POST" enctype="multipart/form-data">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
add users via excell<input name="file" class="form-control" style="padding-bottom:3em; margin-bottom:3em" type="file">
<div style="display:inline;">
<input type="submit" class="btn btn-primary btn-lg" value="ارفع" >
</div>
</form>
And in your routes:
Route::post('ImportUsersFile', ['uses' => 'ExcelUserController#importUser', 'as' => 'importUser']);

Laravel delete item from table list / db

I am trying to delete item from a generated table of items which are from a database table.
My Route:
Route::delete('destroy/{deviceID}', ['as' => 'destroyDevice', 'uses' => 'DeviceController#destroyDevice']);
My Controller method to delete an item:
public function destroyDevice(Request $request, $deviceId = 0)
{
$device = Device::find($deviceId);
if($device)
{
$device->delete();
return redirect()->route('index')->with('success', 'Erfolgreich gelöscht');
}
else
{
return redirect()->route('index')->with('error', 'Fehler');
}
}
And my blade template:
<form action="{{ route('destroyDevice', $deviceValue->id) }}" method="post" name="delete_device">
<input type="hidden" name="_method" value="delete">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<input type="hidden" name="id" value="{{ $deviceValue->id }}">
<td>
<button type="submit" class="btn btn-danger" name="destroy_device">
<span class="glyphicon glyphicon-trash"></span>
</button>
</td>
</form>
If I click on the button nothing happens no error no Response, what am I doing wrong.
If I click on the third delete button the form holds this:
<form action="http://localhost/app/public/device/destroy/3" method="post" name="delete_device"></form>
You can solve this by putting the form inside a td tag in that table.
Like this:
<td> <!-- <--- put these -->
<form action="{{ route('destroyDevice', $deviceValue->id) }}" method="post" name="delete_device">
<input type="hidden" name="_method" value="delete">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<input type="hidden" name="id" value="{{ $deviceValue->id }}">
<button type="submit" class="btn btn-danger" name="destroy_device">
<span class="glyphicon glyphicon-trash"></span>
</button>
</form>
</td> <!-- <--- put these -->
I think the form gets ignored somehow due to not being valid, but I am not 100% sure. May people edit this answer ;)
The parameter is case sensitive so it should be deviceID instead of deviceId
public function destroyDevice(Request $request, $deviceID = 0)
Maybe you have some script that prevents the form to submit, some prevent default maybe on button click or on form submit. Check that.

Laravel Route resource destroy not working

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>

Categories