Laravel 5 Plain Html MethodNotAllowedHttpException - laravel

Hello so I am trying to make an update form in Laravel using plain html. The url of my edit form is http://localhost:8000/profile/sorxrob/edit, sorxrob is the username. And the code in that url is:
<form class="form-horizontal" role="form" action="" method="POST">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<input name="_method" type="hidden" value="PATCH">
//more inputs
</form>
And in my controller named AccountController:
public function update(Request $request, $id)
{
$account = Accounts::findOrFail($id);
$input = $request->all();
$account->fill($input)->save();
return 'success';
}
And I am getting the error MethodNotAllowedHttpException when I click the update button. Is it because my action is equal to nothing? If yes, what is the correct way of routing there?

This is due to your action url which is not correct routing url. Use the following
(1) First define a route in your route.php file
Route::post('profile/{username}/edit', array('as' => 'profile.update', 'uses' => 'AccountController#update'));
(2) Change your action attribute from your form tag
action="{{ URL::route('profile.update', [$username]) }}"
here $username variable will be passed from your AccountController#edit method.

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>

use patch method to update model in laravel

i want to update a customer using patch methodn here is my route
Route::patch('/customers/updateCustomer', 'CustomerController#update')
->name('customers.update');
and here is my form :
<form method="POST" action="{{ route('customers.update') }}">
#csrf
#method('PATCH')
how to pass the id of the customer , the update method requires two parameters, when the first one is the data from the from , the second should be the id of the customer
You should send the customer id with the route() helper function.
Make sure to send the customer object during the form rendering.
<form method="POST" action="{{ route('customers.update', $customer['id']) }}">
#csrf
#method('PATCH')
And modify the route slightly.
Route::patch('/customers/updateCustomer/{customerId}', 'CustomerController#update')->name('customers.update');
add customer id to the route like
Route::patch('/customers/updateCustomer/{id}', 'CustomerController#update')->name('customers.update');
and then your form action should be like
<form method="POST" action="{{ route('customers.update',$customer->id) }}">
you have to send the customer object from where you are returning to the form.
and finally your update function
public function update(Request $request, $id)
{
//your things to do
}
You have to use like this
<form method="POST" action="{{ route('customers.update',['id'=>$customer->id]) }}">
And your route should be
Route::patch('/customers/updateCustomer/{id}', 'CustomerController#update')->name('customers.update');
And your controller should be
public function update(Request $request, $id){
//your code
}

Edit Page takes me to "Page not Found" rather than "Update Route"

When i click on the Edit item Button on "Edit Page" the form submits and takes me to "Page Not Found" rather than update route..
Laravel
<form action="{{ route('admin.products.update', $id) }}" method="post" enctype="multipart/form-data">
{{ csrf_field() }}
<input type="hidden" name="_method" value="PATCH">
// else form elements goes here
</form>
//route file
Route::resource('/products','ProductController');
public function update(Request $request, Product $product)
{
return "hello";
}
i just wanted it to return "hello"
You need to pass the id parameter correctly in your route()
{{ route('products.update', [id => $id]) }}
Admin prefix is applied by the route, you don't need to pass it in route();

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();

How to populate edit form fields in laravel 5.3

I have a problem in populating edit form fields in my first Laravel 5.3 application. In examples I have found online, they have used Form facade to do it. However, I prefer to use plain html.
This is my controller action
public function edit()
{
$profile = Profile::find(Auth::user()->id);
return view('profile.edit', ['profile' => $profile]);
}
In my view file I have a form like this
<form method="POST" action="{{ url('profile/update') }}">
{{ csrf_field() }}
<input id="name" type="text" name="name" value="{{ old('name') }}">
...
</form>
Problem is, above input field is not populated with the value of the $profile->name.
I can get it to work, setting the value attribute like
{{ (old('name')) ? old('name') : $profile->name }}
Since I'm new to Laravel framework I want to know if there is a better way to do that.
Use old($key, $default). It will return $default if $key is not found.

Resources