Cannot send form using POST - laravel

Recently new installation of laravel.
Trying to send a minimal basic form using POST. Get errors as below.
Try below scenarios, but none of them result in successfully processing the form.
My form
<form method="post" action="/form_populating_data">
#csrf_field
{{ method_field('PUT') }}
<?php foreach ($array as $key => $value): ?>
<label
for= <?php echo "'{$key}'" ?>
>
<?php echo "{$key}" ?>
</label>
<input
type="text"
id="1"
value= <?php echo "{$value}" ?>
>
<br>
<?php endforeach; ?>
<input type="submit" name="" value="Save">
</form>
Troubleshooting tests
Startpoint, keep "get" [routes/web.php]
Route::get('/form_populating_data', function () {
return view('site_tax_declarations/form_populating_data');
});
Result:
The POST method is not supported for this route. Supported methods: GET, HEAD.
Adjust, change to "post" [routes/web.php]
Route::post('/form_populating_data', function () {
return view('site_tax_declarations/form_populating_data');
});
Result:
The GET method is not supported for this route. Supported methods: POST.
Adjust [form.php], keep [Route:get]
Added following between POST tags:
#csrf_field
{{ method_field('PUT') }}
Result:
The PUT method is not supported for this route. Supported methods: GET, HEAD.
Adjust [form.php], Change to [Route:post]
Added following between POST tags:
#csrf_field
{{ method_field('PUT') }}
Result:
The PUT method is not supported for this route. Supported methods: POST.
Adjust [form.php], keep [Route:get]
Updated with action="/form_populating_data"
Removed:
#csrf_field
{{ method_field('PUT') }}
Result:
The PUT method is not supported for this route. Supported methods: GET, HEAD.

I think there's a misunderstanding in how Http Request work.
You need one route to deliver the form to the user:
Route::get('/form_populating_data', function () {
return view('site_tax_declarations/form_populating_data');
});
In your form you use a route for processing the data like <form action="/process-data' method="post">.
Then you need a post route with this endpoint:
Route::post("/process-data", function (Request $request) {
dd($request->input());
});
Note: Injecting the Request in the route handler.
The POST input then is available in $request->input().
EDIT: Your form fields need the name attribute like <input name="message">. Then the values are available with $request->input("message")
Additional info you can find in the routing documentation:
https://laravel.com/docs/7.x/routing

you should do it in this way
{{ method_field('PUT') }}
#csrf_field
If you are using PUT Method for updation then you should write it before the csrf field. Method spoofing should be written before csrf.
Hope it will work now.

Related

The POST method is not supported for this route. Supported methods: PUT. LARAVEL

I would like to send data with PUT method
However, this error happens.
Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException
The POST method is not supported for this route. Supported methods: PUT.
I don't really understand why my code is not correct.
web.php
Route::get('/', function () {
return redirect('/home');
});
Auth::routes();
Route::put('/save_data', 'AbcController#saveData')->name('save_data');
view.blade.php
<form action="{{route('save_data')}}" method="POST">
#method('PUT')
#csrf
<input type = "hidden" name = "type" value ='stack' >
<div>
<button>post</button>
</div>
</form>
when it is changed
<input type="hidden" name="_method" value="PUT">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
instead of
#method('PUT')
#csrf
It works well.
make sure to check First Another Route with Same Name
You can make POST route and dont need to put this after form tag #method('PUT')
CHange the Placement of the route before this Auth::routes();
use save_data in route instead of /save_data.
use {{url('save_data')}} in action instead of {{route('save_data')}}.
If you insist on using PUT you can change the form action to POST and
add a hidden method_field that has a value PUTand a hidden csrf field
(if you are using blade then you just need to add #csrf_field and {{
method_field('PUT') }}). This way the form would accept the request.
You can simply change the route and form method to POST. It will work
just fine since you are the one defining the route and not using the
resource group
after all this run artisan command
php artisan route:clear
Change:
<button>post</button>
to:
<button type="submit">post</button>

How to pass variable to view without using url in Laravel?

guys. I'm a newbie of Laravel.
I'm just wondering that if I need to pass some sensitive info, which is been wrote on a form, to controller, then to another view.
How could I pass the info to the view without using URL?
I have made some small test, but the result is all the same (url would include the data).
Here is the code.
Html(a):
<form action="/data" method="GET" class="form-horizontal">
<div class="form-group">
<input type="text" class="form-control" name="Test">
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary">test</button>
</div>
web.php:
Route::get('/data', 'DataController#show');
DataController.php:
public function show(Request $request) {
return view('testShow');
}
Html(b):
<div class="ShowDataHere">
#if(!empty(request()->all()))
{{ request()->Test }}
#endif
Simply change your form to use POST instead of GET.
On the form...
<form action="/data" method="POST" class="form-horizontal">
In your route ...
Route::post('/data', 'DataController#show');
This will send all of the form fields to the server in a way that is less visable (not in the URL) but to make the form secure, you'll want to ensure it's served using HTTPS as well.
In Laravel when you use Route::post('/data', 'DataController#show') the Laravel expect a GET, because of the show function.
So you can use Route::post('/data', 'DataController#someFunction') and in your laravel controller get with:
public function someFunction(Request $request)
{
$array = $request->all(); //your fields
}
In this function you can get the attributes with $array = $request->all();

route is unable to run Controller

I'm new to laravel... created a route and given its a controller which has a method to update a database.. but once it reads the route the app is unable to get to the controller
Route::post('/workorder/store/third/{$id}',
[
'uses'=>'WorkOrdersController#storeThird',
'as'=>'workorder.store.third'
]);
//method in WorkOrderController
public function storeThird(Request $request,$id)
{
$this->validate($request,[
'address_region'=>'required|string',
'address_no'=>'required|string',
]);
$workorder = WorkOrder::find($id);
$workorder->address_region = $request->address_region;
$workorder->address_no = $request->address_no;
$workorder->save();
return view('third-workorder',compact('workorder'));
}
results in browser ...
in the address bar.. "http://localhost:8000/workorder/store/third/9"
and in the browser .."Sorry, the page you are looking for could not be found."
this.. view.blade looks like
<div class="modal" id="createThirdWorkshopModal">
<div class="modal-dialog">
<div class="modal-content">
<form method="POST" action="{{ route('workorder.store.third',['id'=>$workorder->id]) }}" >
{{ csrf_field() }}
Navigating directly to http://localhost:8000/workorder/store/third/9 in your browser is a GET request, but you have the route defined as a route that handles a POST request. You need to submit something for that route to be "found":
<form method="POST" action="{{ url("/workorder/store/third/".$id) }}" ...>
...
</form>
or define the route as
Route::any("/workorder/store/third/{$id}", ...);
to handle this.
Note: ::any() handles all HTTP verbs (GET, POST, PUT, DELETE, etc.)
Router::post('/workorder/store/third/{$id}', WorkOrdersController#storeThird)->name('workorder.store.third');
and use:
<a href="{{route('workorder.store.third', $id)}}">
...
</a>

Laravel 5.4 method not allow on patch

I'm working on updating the data through a PATCH form, the form is working on localhost, but it is not working on server, i have check the route list the route i create is using the PATCH method also, but laravel return me a method not allow exception, here is my code:
Controller:
public function registercert (Request $request, $id) {
// return $request->all();
$user = User::findOrFail($id);
}
Route:
Route::patch('admin/user/registercert/{id}', ['as'=>'registercert', 'uses'=>'admin\AdminUserController#registercert']);
View:
{!! Form::open(['method'=>'PATCH', 'action'=>['admin\AdminUserController#registercert',$user_id], 'enctype'=>'multipart/form-data']) !!}
{!! csrf_field() !!}
...
{!! Form::close() !!}
I had a similar problem, I fixed it using a "regular" form with a POST method and adding laravel's method spoofing
<form class="form" action="/clientes/{{ $cliente->id }}" method="POST" enctype="multipart/form-data" >
{{ method_field('PUT') }}
#include('partial.cliente-campos')
</form>
The important part here is the method="POST" in the form and the {{ method_field('PUT') }}. You need both.

Laravel form won't PATCH, only POST - nested RESTfull Controllers, MethodNotAllowedHttpException

I am trying to allow users to edit their playlist. However, whenever I try to execute the PATCH request, I get the MethodNotAllowedHttpException error. (it is expecting a POST)
I have set up RESTful Resource Controllers:
Routes.php:
Route::resource('users', 'UsersController');
Route::resource('users.playlists', 'PlaylistsController');
This should give me access to: (as displayed through php artisan routes)
URI | Name | Action
PATCH users/{users}/playlists/{playlists} | users.playlists.update | PlaylistsController#update
However, when I try to execute the following form, I get the MethodNotAllowedHttpException error:
/users/testuser/playlists/1/edit
{{ Form::open(['route' => ['users.playlists.update', $playlist->id], 'method' => 'PATCH' ]) }}
{{ Form::text('title', $playlist->title) }}
{{ Form::close() }}
If I remove 'method'=> 'PATCH' I don't get an error, but it executes my public function store() and not my public function update()
In Laravel 5 and up:
<form method="POST" action="patchlink">
#method('patch')
. . .
</form>
Write {!! method_field('patch') !!} after form:
<form method="POST" action="patchlink">
{!! method_field('patch') !!}
. . .
</form>
Official documentation for helper function method_field()
Since html forms support only GET and POST you need to add an extra hidden field
to the form called _method in order to simulate a PATCH request
<input type="hidden" name="_method" value="PATCH">
As suggested by #Michael A in the comment above, send it as a POST
<form method="POST" action="patchlink">
<input type="hidden" name="_method" value="PATCH">
Worked for me.

Resources