Update method in html form in laravel - 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>

Related

a form with the PUT method is sent with the GET method

I have 2 controller with resource clients and sumsubs.
being on the clients.show route I created a form that I would like to send to the sumsubs.update endpoint but it does not work; the form still contacts sumsubs.show
here are the routes that I have defined
here is the form
<form accept-charset="UTF-8" class="form-horizontal" name='recheck' method="PUT" action="{{ route('sumsubs.update', $client->id) }}">
#csrf
<div class="form-group row">
<div class="col-sm-10">
<input type="number" class="form-control" id="candidat" name="candidat" value="{{ $client->id }}" hidden required>
</div>
</div>
<div class="card-footer">
<button type="submit" class="btn btn-success float-right"> {{ __('Recheck') }} </button>
</div>
</form>
the following is my sumbsubs controller
public function show($id)
{
return 'not ok';
}
public function update(Request $request, $id)
{
return 'ok ';
}
this is what i get when i submit the form
I would like to know where is my mistake
I read the laravel documentation for form submission and tried with url and route methods but I still get the same result
You have to use method spoofing.
Change your method in form tag to Post:
<form accept-charset="UTF-8" class="form-horizontal" name='recheck' method="POST" action="{{ route('sumsubs.update', $client->id) }}">
and add this after #csrf:
#method('PUT')

Why am i getting an error 404 when i run this code in laravel 9?

I was watching an old tutorial about laravel 7 whiles using laravel 9, i tried to create a HTML form like this.
<div class="card-body">
<form action="/upload" method="post" enctype="multipart/form-data">
#csrf
<input type="file" name="image">
<input type="submit" value="upload">
</form>
</div>
then in my route(web.php) i added a code like this
route::post('/upload', function(Request $request)
{$request->image->store('images', 'public');
return 'image uploaded succesfully';
but in my webiste it tells me page the url you requested is not found on the site serve
You've defined your route using POST meaning it will only respond to POST requests.
If you try to access /upload from a web browser, that uses a GET request which you've not defined a route for. So you want to define such a route:
Route::get('/upload', function () {
return view('upload.blade.php');
});
You'll want to replace upload.blade.php with the name of your view that has your upload form in it.
Name Your Route 1st , hit this command php artisan route:clear then try..
Change the form action action="{{ route('upload') }}"
<div class="card-body">
<form action="{{ route('upload') }}" method="post" enctype="multipart/form-data">
#csrf
<input type="file" name="image">
<input type="submit" value="upload">
</form>
</div>

The PUT method is not supported for this route. Supported methods: GET, HEAD

I'm trying to learn Laravel, and I'm following a series of tutorials called laracast. I'm at episode 24, "Forms that submit PUT requests. The short story is that the markup uses a hidden value to set the method to PUT, although the forms method is set to POST. Still, when I do this, I get the error message from the title:
Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException
The PUT method is not supported for this route. Supported methods: GET, HEAD.
From the tutorials, I'd expect POST to also be a supported method. However, when I try to fix this, all resources I can find simply tells me what I already know. PUT is not supported, but I can fake it/override it, and then they refer to what I have already done... Are there any other reasons why I might get this error message?
HTML Form:
<form method="POST" action="/competition-categories">
#csrf
#method('PUT')
<div class="form-group row">
<label for="competition-category-name-input" class="col-4 col-form-label">Name</label>
<div class="col-8">
<input id="competition-category-name-input" name="competition-category-name-input" type="text" class="form-control" required="required" value="{{ $competitionCategory->name }}">
</div>
</div>
<div class="form-group row">
<label for="competition-category-abbreviation-input" class="col-4 col-form-label">Abbreviation</label>
<div class="col-8">
<input id="competition-category-abbreviation-input" name="competition-category-abbreviation-input" type="text" class="form-control" required="required" value="{{ $competitionCategory->abbreviation }}">
</div>
</div>
<div class="form-group row">
<div class="offset-4 col-8">
<button name="submit" type="submit" class="btn btn-primary">Submit</button>
</div>
</div>
</form>
web.php snippet:
//Competition Categories
Route::get('/competition-categories', 'CompetitionCategoryController#index');
Route::get('/competition-categories/create', 'CompetitionCategoryController#create');
Route::get('/competition-categories/{competitionCategory}', 'CompetitionCategoryController#show');
Route::get('/competition-categories/{competitionCategory}/edit', 'CompetitionCategoryController#edit');
Route::post('/competition-categories/{competitionCategory}', 'CompetitionCategoryController#store');
Route::put('/competition-categories/{competitionCategory}', 'CompetitionCategoryController#udpate');
Route::delete('/competition-categories/{competitionCategory}', 'CompetitionCategoryController#destroy');
Snippet from the controller:
public function update(Request $request, CompetitionCategory $competitionCategory)
{
$competitionCategory->update($this->validateCompetitionCategory());
return redirect()->route('competition-categories' , [$competitionCategory]);
}
You're forgetting the id in form, this should fix your problem:
action="/competition-categories/{{$competitionCategory->id}}"
The most common thing that this happens is your cache. When you add a new route or change something in your routes, always run after php artisan optimize to refresh you cache.
I recommend using the named-routes for more informations and messages see =>
https://laravel.com/docs/7.x/routing#named-routes

Change PUT request to POST request to include image update

I wrote a post edit method which at first consisted only of text. I did the update using a PUT request.
Now I want to include images to my posts, so I added it, and it works for my POST request of creating a post but doesn't work for my update post, when I want to change image, since PUT request doesn't support file uploads.
So now I'm stuck trying to change my update method from PUT request that only updates text to a POST request that updates both the text and an image if supplied.
This is the code I wrote so far:
public function update(Request $request, Post $post)
{
//store updated image
if($request->hasFile('image') && $request->file('image')->isValid()){
if($post->hasMedia('posts')) {
$post->media()->delete();
}
$post->addMediaFromRequest('image')->toMediaCollection('post', 's3');
}
$post->update(request()->validate([
'body' => 'required'
]));
return redirect($post->path());
}
I think that the $post->update doesn't work for the POST request. I just want to update a text if an update was given.
Using Laravel 6.
EDIT: My form layout structure (simplified)
<form action="action="/posts/{post}" method="POST">
#method('PUT')
#csrf
<div class="form-group row">
<input id="body" type="text" class="form-control" name="body" value="{{ old('body', $post->body) }}">
<input id="image" type="file" class="form-control" name="image">
<button type="submit" class="btn btn-primary">Update Post</button>
</form>
My routes:
Route::get('/posts/{post}/edit', 'PostsController#edit')->name('posts.edit');
Route::put('/posts/{post}', 'PostsController#update');
I tried your code and it worked fine with me , only added #csrf in form tag.
<form action="/posts/{post}" method="POST" enctype="multipart/form-data">
#csrf
#method('PUT')
<div class="form-group row">
<input id="body" type="text" class="form-control" name="body" value="{{ old('body', $post->body) }}">
<input id="image" type="file" class="form-control" name="image">
<button type="submit" class="btn btn-primary">Update Post</button>
</form>

submit form can not access method in controller

When I submit form in view, I can't access that in controller.
View create.blade.php
#extends('layouts.app')
#section('content')
<form method="POST" action="posts">
<input type="text" name="title" placeholder="Enter title">
<input type="submit" name="submit">
</form>
Controller PostController
public function store(Request $request)
{
return $request->all();
}
I want to access the form data, in PostController in the store method and once I click submit, it not giving the result.
I have route: Route::resource('posts', 'PostController') and I am posting to this route/url, as action="posts". In Route I have used resource and in method="post"
Does it matter? What can I do?
Looks like you are missing the action
Route::post('post', 'PostController#store');
I'm not sure but may be it's because of url and not included csrf_field(). Try changing it to:
<form method="POST" action="{{ url('posts') }}">
{{ csrf_field() }}
<input type="text" name="title" placeholder="Enter title">
<input type="submit" name="submit">
</form>
or you may use route('posts.store ') instead of url('posts')

Resources