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

I get the following error with the code below.
The DELETE method is not supported for this route. Supported methods:
GET, HEAD, POST
Controller
public function destroy(Post $post)
{
$post->delete();
return back();
}
Route
Route::delete('/posts/{post}', [PostController::class, 'destroy']);
View
<form action="{{ route('posts', $post) }}" method="POST" >
#csrf
#method('delete')
<button type="submit" class="text-blue-500">Delete</button>
</form>

Route:
Route::delete('/admin/user_list/{id}', [UserController::class, 'destroy'])->name('admin.user_list');
Controller:
public function destroy(Request $request, $id)
{
User::where('id', $id)->delete();
return redirect()->back()->withSuccess('Your record deleted successfuly');
}
View:
form action="{{ route('admin.user_list', $rows->id) }}" method="post"
#method('DELETE')
#csrf
<button onclick="return confirm('Are you sure you want to delete this?');" type="submit" value="delete" class="btn btn-danger btn-xs">
<span>DELETE</span>
</button>

Related

Pass multiple parameter to route

I have a route like below.
Route::resource( 'mosque-build', 'MosqueBuildController' );
My blade file code is like below.
<form id="delete-form-{{ $collection->id }}" action="{{ route('admin.mosque-build.destroy',$collection->id,'target') }}" method="POST" style="display: none;">
#csrf
#method('DELETE')
</form>
My controller function is like below.
public function destroy($id,$type)
{
if($type == 'target') {
$target_collection = Targetcollection::find($id);
$target_collection->delete();
return redirect()->route( 'admin.mosque-build' );
}
}
I am getting error local.ERROR: Too few arguments to function.

sending get parameter from a input text in Laravel 8

I need to collect a get variable in Laravel 8 passed as follows:
127.0.0.1/search/keywordToSearch
I have this route created in my web.php file:
Route::get('/search/{keyword}', [SearchController::class, "search"])->name('search');
My form is:
<form class="form-horizontal" action="{{ route('search') }}" >
<fieldset>
<div class="form-group">
<label for="keyword" class="col-lg-2 control-label">Keyword</label>
<div class="col-lg-10">
<input type="text" class="form-control" id="keyword" name="pelicula" placeholder="keyword">
</div>
</div>
<div class="form-group">
<div class="col-lg-10 col-lg-offset-2">
<button class="btn btn-default">Cancel</button>
<button type="submit" class="btn btn-primary">Search</button>
</div>
</div>
</fieldset>
</form>
But when I push the send button the url obtained is this:
http://127.0.0.1:8000/search?keyword=safafs
How I must do about passing the input value to my controller?
I think in this particular scenario you don't need to pass a parameter trough the route. Make it simple, like
Route::get('/search', [SearchController::class, "search"])->name('search');
In the controller declare the action like
public function search(Request $request)
{
dd($request->pelicula); // here you have your keyword data
}

onget handler razor pages

I have a button on a GET Form on asp.net core razor pages
<form method="get" asp-page="Index">
<button type="submit" asp-page="Index" asp-page-handler="Something" class="btn btn-primary"></button>
</form>
and the code behind
public IActionResult OnGetSomething(){
//... some code
return Page();
}
My problem is the onget handler code is never executed
If the form is POST the onpost handler will work fine but if it is GET it doesn’t work
So what am I missing here? and how to make the onget handler work?
When you submit a form using GET, the browser trashes query string values in the form action and replaces them with a new query string built from the form fields. You can hit your handler by adding an appropriate hidden field to the form:
<form method="get" asp-page="Index">
<input type="hidden" name="handler" value="Something" />
<button type="submit" class="btn btn-primary">Get</button>
</form>
you can't go to get method because of parameters miss matching.
Suppose:
In controller
[HttpGet]
public IActionResult Create()
{
return View();
}
//Post Method
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create(string Name)
{
}
In View:
<form asp-action="Create">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<label asp-for="Name" class="control-label"></label>
<input asp-for="Name" class="form-control" />
<span asp-validation-for="Name" class="text-danger"></span>
</div>]
<div class="form-group">
<input type="submit" value="Create" class="btn btn-primary" />
</div>
</form>
Then it go post method cause it has 1 parameters (Name). if we not net any input or parameters are not same then it hit different method.
It the concept of method overloading
In details of method overloading https://www.geeksforgeeks.org/c-sharp-method-overloading/

Why did i get Status Code: 405; Method Not Allowed after trying to execute the delete action?

ASP.NET Core 5.0, EF Core 5.0
My action :
[HttpPost("id")]
public IActionResult DeleteEquipment(int id)
{
repository.DeleteEquipment(id);
return View("WorkoutDB");
}
My ef method:
public void DeleteEquipment(int Id)
{
_context.Remove(new Equipment() { EquipmentId = Id });
_context.SaveChanges();
}
My button from partial view:
<input type="button" value="Delete" class="btn btn-danger" onclick="location.href='#Url.Action("DeleteEquipment", "Admin", item.EquipmentId)'" /
I don't know is it normal code or not, i am newbie, but i spent 4 hours and couldn't get the result.
Thanks for you attention!
UPD:
I changed my button
<td>
<form asp-action="DeleteEquipment" method="post">
<input type="hidden" name="EquipmentId" value="#item.EquipmentId" />
<button type="submit" class="btn btn-danger btn-sm">
Delete
</button>
</form>
</td>
But its still not working. I cant bind correct value to EquipmentId. Always 0 value. Why?
location.href='#Url.Action("DeleteEquipment", "Admin", item.EquipmentId)' cause browser to redirect user to new url. Redirect cause GET Request but your DeleteEquipment method has HttpPost attribute.
Replace
[HttpPost("id")]
with
[HttpGet("{id}")]
As #mason mentioned if you need to call your api with POST Request you need to use form.
<form asp-action="DeleteEquipment" asp-controller="Admin" asp-route-id="#item.EquipmentId">
<input type="submit" class="btn btn-danger" value="Delete" />
</form>

I cant delete the first row that was added to the cart laravel 5.8 bumbummen99 library

I cant delete the first row from the item added to cart. I used Laravel 5.8 with bumbummen99/shoppingcart. Other rows can be deleted successfully but
I can't delete the first row. Please I need help
<form action="{{ url('cart', [$item->rowId]) }}" method="POST">
{!! csrf_field() !!}
<input type="hidden" name="_method" value="DELETE">
<td><button type="submit" class="btn btn-primary btn-sm">X</button></td>
</form>
Above is my view
Below is my route
Route::delete('/cart/{product}', 'CartController#destroy')->name('cart.destroy');
Below is my controller with the destroy method
public function destroy($id)
{
Cart::remove($id);
return back()->with('success', 'Item has been removed');
}
Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException
The DELETE method is not supported for this route. Supported methods: GET, HEAD, POST.
The problem may be solved by changing your method attribute on your <form> to from POST to DELETE:
<form action="{{ url('cart', [$item->rowId]) }}" method="DELETE">
{!! csrf_field() !!}
<input type="hidden" name="_method" value="DELETE">
<td><button type="submit" class="btn btn-primary btn-sm">X</button></td>
</form>