Laravel 5.4 method not allow on patch - methods

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.

Related

Cannot send form using POST

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.

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>

Undefined variable in view laravel

I have this code here which is listing all items from one user, user and item are two differents model and they have relationship with their primary keys.
#foreach($user->items as $item)
<p class="heading">
{{ $item->item_id }}
</p>
#endforeach
My question is how to send that item_id with post request to controller, because when I write like this for example <form class="form-horizontal" method="post" action="{{ route('preview', $item->item_id) }}" role="form" enctype="multipart/form-data"> it gives me that $item variable is not defined.
My controller:
public function preview($item_id){
return view('itempreview')->with('item_id', $item_id);
}
Any ideas how to send that item_id?
You should use array as 2nd param to route() method!
You are doing it wrong, you should send it like this:
route('preview', ['item_id' => $item->item_id]);
Make sure, your route should be defined as this:
Route::post('preview/{item_id}', 'ControllerName#preview')->name('preview');
Hope this helps!
You need to send $item_id as part of the POST:
#foreach($items as $item)
<form method="post" action="{{ route('preview') }}">
<input type="hidden" name="item_id" value="{{ $item->item_id }}">
</form>
#endforeach
Then in the method that receives the POST:
public function index() {
$item_id = request()->input('item_id');
return view('preview');
}

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