getting errors The POST method - laravel

I have a problem with my edit page. When I submit I get this error:
Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException
The GET method is not supported for this route. Supported methods:
POST
.
I have no clue where it comes from as I am pretty new to Laravel.
web.php
Route::post('/admin/add_reseller','ResellerController#addReseller');
Controller.php
public function addReseller(){
return view ('admin.resellers.add_reseller');
}
add_reseller.blade.php
<form class="form-horizontal" method="post" action="" name="addreseller" id="addreseller">
{{ csrf_field() }}

Tip:
First of all, I would use named routes, that means you add ->name('someName') to your routes. This makes the use of routes way easier and if you decide that your url doens't fit you don't need to change it everywhere you used the route.
https://laravel.com/docs/7.x/routing#named-routes
e.g. Route::post('/admin/add_reseller',
'ResellerController#addReseller')->name('admin.reseller');
Problem:
What I see is, that you lack the value for the action attribute in your <form>. The action is needed, so that the right route is chosen, if the form gets submitted.
Solution:
I guess you just have to add action="{{route('admin.reseller')}}" for the attribute, so that the request goes the right route.

<form class="form-horizontal" method="post" action="" name="addreseller" id="addreseller">
Your action in the form is empty, you need to add the corresponding route there.
I'd suggest you to use named routes.
https://laravel.com/docs/7.x/routing#named-routes
In web.php
Route::post('/admin/add_reseller','ResellerController#addReseller')->name('admin.add.reseller');
and then in your blade file you could refer to the route using the route() function by passing the name of the route as an argument
<form class="form-horizontal" method="post" action="{{route('admin.add.reseller')}}" name="addreseller" id="addreseller">

Related

Laravel 9 - Submitting a form to a route leads to /null and doesn't do anything

I'm using soft deletes and trying to make a function to restore the deleted row. Really everything to do with submitting the form doesn't work, even deleting...
My tags are within a forelse loop, maybe that's the cause??
Route is (this is above my resource route):
Route::post('/post/restore/{id}', [PostController::class, 'restore'])
->name('post.restore');
Controller is:
public function restore($id)
{
dd($id);
}
Form/view is:
<form action="{{route('post.restore', $post->id)}}" method="POST">
#csrf
<button type="submit" class="dropdown-item popup" data-confirm="Would you like to restore?">Restore</button>
</form>
After submitting, it just takes me to:
domainURL/post/null
and gives a 404 error
Any advice?? I also tried it without the {id} at the end of the route, same results
I think you are using the wrong syntax for route function, instead of this:
route('post.restore', $post->id)
you should use it like this:
route('post.restore', ['id' => $post->id])
you can read more here as well.
I figured it out... my "would you like to restore?" javascript function was breaking the form. I was using javascript to popup a confirmation message before submitting and during that process it lost the ID
I got rid of that and it works fine
Thank you!

Redirecting To Controller Actions with POST method in laravel 6

I have an action named destroy in UserController I don't wanna work this action instead of UserInfoController#destroy supposed to run. So I need to redirect to the UserInfoControlle#destroy controller.
UserController#destroy action;
return redirect()->action(
'UserInfoController#destroy',['id' => 1]
);
Action successfully ran but I get this error;
The GET method is not supported for this route. Supported methods: POST.
you have some way to do that like below:
make new get route for that
Route::get('/delete/{id}','UserInfoController#destroy')->name('deleteWithGetMethod');
change post to any in your route
Route::any('/delete/{id}','UserInfoController#destroy')->name('delete');
return a view that contains below code
<form id="myForm" action="{{ route('delete',$userInfoId) }}" method="post">
</form>
<script type="text/javascript">
document.getElementById('myForm').submit();
</script>

update method issue(The POST method is not supported) in laravel

I want to create update method and this is the code:
Route::get("/allProducts/edit/{id}","AllproductController#edit")->name('Allproduct.edit');
Route::post("/allProducts/update/{id}","AllproductController#update")->name('Allproduct.update');
<form class="form-horizontal tasi-form" method="post" enctype="multipart/form-data"
action="{{ route('allProducts.update' , [ 'id'=>$allproduct->id ]) }}">
{{ csrf_field()}}
public function update(Request $request, $id)
{
$data = Allproduct::find($id);
$data->name = $request->name;
$data->save();
return redirect(route('allProducts.index'));
}
when I click on submit button it shows me :
"The POST method is not supported for this route. Supported methods: GET, HEAD, PUT, PATCH, DELETE" error!
what is the problem?
Your route names do not match.
in routes:
name('Allproduct.update');
in the form:
allProducts.update
Also, you can always check the name of the routes thanks to the console command:
php artisan route:list
if you want use method PUT:
you can change method in routes:
Route::post to Route::put
and add next in form:
<input type="hidden" name="_method" value="PUT">
OR
#method('PUT')
this is if your laravel version is 6 and if your version another, check correct way to use PUT method in forms at laravel.com/docs/6.x/routing with your version.
As said here
HTML forms do not support PUT, PATCH or DELETE actions. So, when defining PUT, PATCH or DELETE routes that are called from an HTML form, you will need to add a hidden _method field to the form. The value sent with the _method field will be used as the HTTP request method:
So your form should look like this:
<form class="form-horizontal tasi-form" method="post" enctype="multipart/form-data"
action="{{ route('Allproduct.update' , [ 'id'=>$allproduct->id ]) }}">
#csrf
#method('PUT')
You had a typo in your route name and it was missing the method field.
Change your route to this:
Route::put("/allProducts/update/{id}","AllproductController#update")->name('Allproduct.update');
This will work, but
i strongly recommend you read this laravel naming conventions, and then change the name of your controller and model to AppProductController, AppProduct.

Laravel - route("resource.destroy") calls "resource.show"

This is the web.php
Route::group(['middleware' => 'auth'],
function () {
Route::get('letters/getRows', 'LetterController#getRows')->name('letters.getRows');
Route::get('letters/{letter}/A4', 'LetterController#A4')->name('letters.A4');
Route::get('letters/{letter}/A5', 'LetterController#A5')->name('letters.A5');
Route::resource('letters', 'LetterController');
}
);
I created a link as follow
"<a class='mx-2 h5' href='".route('letters.destroy', $entity->id)."'><i class='icon-remove-circle'></i></a>".
where the $entity->id is the id of the letter. The problem is, it links to show method not the destroy method. What can I do?
Using a form like this
{{ Form::open(array('route' => array('letters.destroy', $entity->id), 'method' => 'delete')) }}
<button type="submit" >Delete Account</button>
{{ Form::close() }}
may solve the problem but I want to use a tag not a form.
update
In the php artisan route:list, the url of destroy and show are the same
thanks
When you use the Route::resource method it will create, among others, a route to DESTROY a resource like this: /letters/:id/ and another route to EDIT the resource: /letters/:id, and one more to SHOW /letters/:id
They all look the same. However, the difference is in the HTTP method/verb used to reach each route.
If you look to the output if php artisan route:list, you will find the list of HTTP methods used. Something like:
GET|HEAD | letters/{letter} | letters.show
PUT|PATCH | letters/{letter} | letters.update
DELETE | letters/{letter} | letters.destroy
Therefore, to show a letter, you use a GET method, to edit a letter, use PUT method, and to destroy/delete, you use a DELETE method.
When you use an a tag, the browser will use the GET method, thus will reach the letters.show route. Html forms, can use POST or GET. Finally to use the DELETE http method, you need a form with hidden input named _method and the value="delete inside the form. Check the docs for more details.
There is also a note about this in LaravelCollective package documentations
Note: Since HTML forms only support POST and GET, PUT and DELETE methods will be spoofed by automatically adding a _method hidden field to your form.
Finally, if you must use an anchor tag <a>, you could use javascript to listen to the click event and submit a form with DELETE method.
Update to add an example:
You can find an example of using an anchor tag to submit the form, in the default app layout in the framework here
And this is a modified version to submit a delete request:
<a class="dropdown-item" href="#"
onclick="event.preventDefault();
document.getElementById('destroy-form').submit();">
{{ __('DELETE') }}
</a>
<form id="destroy-form" action="{{ route('letters.destroy', $entity) }}" method="POST" style="display: none;">
#method('DELETE')
#csrf
</form>
You cant. If you want to make a DELETE request you need to spoof it via a form (method POST, _method DELETE) or use Javascript.
Hyperlinks will cause new requests which will be GET requests. That is just how the web works.

How does Laravel handle PUT requests from browsers?

I know browsers only support POST and GET requests, and Laravel supports PUT requests using the following code:
<?= Form::open('/path/', 'PUT'); ?>
... form stuff ...
<?= Form::close(); ?>
This produces the following HTML
<form method="POST" action="http://example.com/home/" accept-charset="UTF-8">
<input type="hidden" name="_method" value="PUT" />
... form stuff ...
</form>
How does the framework handle this? Does it capture the POST request before deciding which route to send the request off to? Does it use ajax to send an actual PUT to the framework?
It inserts a hidden field, and that field mentions it is a PUT or DELETE request
See here:
echo Form::open('user/profile', 'PUT');
results in:
<input type="hidden" name="_method" value="PUT">
Then it looks for _method when routing in the request.php core file (look for 'spoofing' in the code) - and if it detects it - will use that value to route to the correct restful controller.
It is still using "POST" to achieve this. There is no ajax used.
Laravel uses the symfony Http Foundation which checks for this _method variable and changes the request to either PUT or DELETE based on its contents. Yes, this happens before routing takes place.
You can also use an array within your form open like so:
{{ Form::open( array('route' => array('equipment.update', $item->id ),
'role' => 'form',
'method' => 'put')) }}
Simply change the method to what you want.
While a late answer, I feel it is important to add this for anyone else who finds this and can't get their API to work.
When using Laravel's resource routes like this:
Route::resource('myRoute','MyController');
It will expect a PUT in order to call the update() method. For this to work normally (outside of a form submission), you need to make sure you pass the ContentType as x-www-form-urlencoded. This is default for forms, but making requests with cURL or using a tool like Postman will not work unless you set this.
PUT usually refers to update request.
When you open a form inside laravel blade template using,
{{ Form::open('/path/', 'PUT') }}
It would create a hidden field inside the form as follows,
<input type="hidden" name="_method" value="PUT" />
In order for you to process the PUT request inside your controller, you would need to create a method with a put prefix,
for example, putMethodName()
so if you specify,
{{ Form::open('controller/methodName/', 'PUT') }}
inside Form:open. Then you would need to create a controller method as follows,
class Controller extends BaseController {
public function putMethodName()
{
// put - usual update code logic goes here
}
}

Resources