MethodNotAllowedHttpException Laravel method PUT not working - laravel

Basically I am using modal-dialog as shown below and I'm trying to get the method PUT to work. Unfortunately, I've tried either ways with _method="PUT" but it still doesn't work, anyone has a solution for this?
<div class="modal-body">
<form class="form-horizontal" role="form" method="POST" _method="PUT" action="/manage_accounts/{{ $user->id }}" novalidate>
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<div class="form-group">

The problem is that HTML does not support PUT methods natively so you can't do something like this:
<form method="PUT"...
There is a workaround for this. Laravel accepts PUT, PATCH, and DELETE methods by adding a hidden input field. In other words, something like this:
<form class="form-horizontal" role="form" method="POST" action="/manage_accounts/{{ $user->id }}" novalidate>
<input type="hidden" name="_method" value="PUT">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
Notice that you are POSTing to the server, but you can add a hidden input field named _method for PUT, PATCH, and DELETE methods.

You are probably PUTting to a GET route.
Make sure that the '/manage_accounts/{id}' route is setup as a PUT in your routes file.
Route::put('/manage_accounts/{id}', function()
{
//
});

Related

Laravel datatables raw columns

i have a code like this to display an action button for datatables
->addColumn('action', function () {
return '<form id="delete" action="{{ route(' . 'admin.posts.destroy' . ', $model) }}" method="POST">
#csrf
#method("DELETE")
Edit
<input type="submit" class="btn btn-danger" value="Delete">
</form>';
})
But the #csrf and #method("DELETE") become a string/text (not method). I tried to append {{ }} in #csrf and #method("DELETE") but it doesn't work. How to change that text to method in blade templates without make a new view for action button like that?
Thank you!
#csrf replace with <input type="hidden" name="_token" value="{{ csrf_token() }}"> and
#method("DELETE") with <input type="hidden" name="_method" value="delete">

The GET method is not supported for this route. Supported methods: PATCH. when doing update

i want to submit for updating the data, i already use the patch method, but it keep telling me that the get method is not supported
the edit formedit.info.blade
<form action="{{ route('info.update', ['info' => $info->id]) }}" method="patch" enctype="multipart/form-data">
<input type="hidden" name="_method" value="patch">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
the route list
web.php
//info penting
route::get('/info-admin','InfosController#index')->middleware('auth','admin')->name('admin.info-admin');
route::get('/tambah-info','PagesController#tambah')->middleware('auth','admin')->name('info.add');
route::patch('/update-info/{info}/update','InfosController#update')->name('info.update');
route::get('/edit-info/{info}/edit','InfosController#edit')->middleware('auth','admin')->name('info.edit');
route::delete('/info/{id}','InfosController#destroy')->middleware('auth','admin')->name('info.destroy');
here's for update logic
InfosController
public function update(Request $request, Info $info) {
Info::where('id', $info->id)->update([
'judul' => $request->judul,
'konten' => $request->konten,
'image' => $request->image,
]);
return redirect('')->route('admin.info-admin')->with('success', 'Successful');
}
what did i do wrong?
HTML5 forms only supports GET, POST and DIALOG method only. so using PATCH won't work.
you have to add it inside the from
<form action="{{ route('info.update', ['info' => $info->id]) }}" method="POST" enctype="multipart/form-data">
#csrf
#method('PATCH')
this link will help you to understand form methods.
You have to change the method to POST. Because in some browser PUT/PATCH is not suported
<form action="{{ route('info.update', ['info' => $info->id]) }}" method="post" enctype="multipart/form-data">
<input type="hidden" name="_method" value="patch">
<input type="hidden" name="_token" value="{{ csrf_token() }}">

Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException The GET method is not supported for this route. Supported methods: PUT

view
web.php
[productsController
Change your web.php file
Route::post('/update/{id}','productsController#update')->name('product.update');
In your view
<form action="{{route('product.update',['id' => $products->id])}}" method="post">
Replace #method('PUT') with
<input name="_method" type="hidden" value="PUT">
Remove method="PUT" and place method="POST" from <form ...> tag. Also, I'm not sure about this #method('PUT'). I will use something like this: <input type="hidden" name="_method" value="PUT"/>

Laravel 5.8 #csrf and #method not working

When using the #csrf and the #method('') it shows correctly on the page code but doesn't work. Either fails with 419 error code or the original method isn't allowed for this page type error.
Using the "old" code seems to work
#csrf
{{ csrf_field() }}
#if(isset($post))
#method('PUT')
{{method_field('PUT')}}
#endif
The csrf_field and method_field options work even though they show the same values in the page code.
<input type="hidden" name="_token" value="hTLipJaANmyR2I9VPyF7QvUKDiQlte7BAoRLjqeN">
<input type="hidden" name="_token" value="hTLipJaANmyR2I9VPyF7QvUKDiQlte7BAoRLjqeN">
<input type="hidden" name="_method" value="PUT">
<input type="hidden" name="_method" value="PUT">

Update method in html form in laravel

I'm trying to update inputs using html form in laravel:
<form action="{!! route('users.update',['id' => $users->id]) !!}" method="post">
<div class="form-group row">
<label for="colFormLabelLg" class="col-sm-3 col-form-label col-form-label-lg">customer_name</label>
<div class="col-sm-10">
<input value="{{$name}}" class="form-control form-control-lg" placeholder="col-form-label-lg">
</div>
<button type="submit" class="btn btn-primary btn-lg" > Edit</button>
</form>
Everything in the controller work perfectly however in the view page I received this error:
Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException
No message
What am I doing wrong?
Please correct your route as POST like:
Route::post('update/{id}', 'YourController#update')->name('users.update');
You need to spoof the method as you are using to post the data. Since HTML forms can't make PUT, PATCH, or DELETE requests, you will need to add a hidden _method field to spoof these HTTP verbs. The #method Blade directive can create this field for you like this:
<form action="/foo/bar" method="POST">
#method('PUT') //add this to your form
</form>
or
<form action="/foo/bar" method="POST">
{{ method_field('patch')}} //add this to your form
</form>
You need to put #csrf and #method('PATCH') inside your form view.
I have the same problem and I had it worked after adding some lines in my code:
Just use $users->id instead of doing it as array ['id' => $users->id]
Use csrf and spoof the method by adding #method('PUT')
Your code should look like this:
<form action="{!! route('users.update', $users->id) !!}" method="post">
#csrf
<!--Some fields-->
#method('PUT')
</form>

Resources