Laravel 5.2 Form update error - php

Okay so guys i've a problem when working with laravel's update & i've spent 3 days and no one can solve my problem too so i decide to ask you all pro coder, please kindly help me
First, this is part of my View (editkeluhan.blade.php)
<form class="form-horizontal" role="form" method="POST" action="{{ url('/editkeluhanadmin/{$keluhan->id}') }}" enctype="multipart/form-data">
{{ csrf_field() }}
<div>
<input name="_method" type="hidden" value="PATCH">
</div>
Second, This is my Route
Route::get('editkeluhan/{id}','AdminController#editkeluhan');
Route::post('editkeluhanadmin/{id}', 'AdminController#updatekeluhanadmin');
public function editkeluhan($id){
$halaman="tindaklayanan";
$keluhan=keluhan::findOrFail($id);
return view('layanankonsumen.editkeluhan',compact('keluhan','halaman'));
}
public function updatekeluhanadmin(Keluhan $keluhan, Request $r){
$halaman = 'tindaklayanan';
$keluhan->update($r->all());
return redirect('/');
Third, this is AdminController
public function editkeluhan($id){
$halaman="tindaklayanan";
$keluhan=keluhan::findOrFail($id);
return view('layanankonsumen.editkeluhan',compact('keluhan','halaman'));
}
public function updatekeluhanadmin(Keluhan $keluhan, Request $r){
$halaman = 'tindaklayanan';
$keluhan->update($r->all());
return redirect('/');
}
This is ERROR, but when i change my Route into this
Route::resource('editkeluhanadmin', 'AdminController#updatekeluhanadmin');
Error is gone BUT Its not update in database
Please Please Help me

You use PATCH action in your form
<input name="_method" type="hidden" value="PATCH">
so you need to change your route to:
Route::patch('editkeluhanadmin/{id}', 'AdminController#updatekeluhanadmin');

Visit https://laracasts.com/discuss/channels/laravel/update-form-data-laravel-52 to see my disscussion there & the answer if anyone need, good luck.

Related

PUT method not supported for the route when trying to do an update method

This is what my code looks like
Route:
Route::put('/articles/{$article}', 'ArticlesController#update');
Controller:
public function update($id){
$article=Article::find($id);
$article ->title = request('title');
$article->excerpt=request('excerpt');
$article->body=request('body');
$article->save();
return redirect('/articles/'. $article->id);
}
Blade:
<form method="POST" action="/articles/{{$article->id}}" >
#csrf
#method('PUT')
And everytime I try to submit an update I get this:
The PATCH method is not supported for this route. Supported methods: GET, HEAD.
I'm currently stuck on this one.
Try this
<form action="/articles/{{$article->id}}" method="POST">
<input type="hidden" name="_method" value="PUT">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
</form>
and the route
Route::put('/articles/{article}', 'ArticlesController#update');
It's better to do:
in routing
Route::put('/articles/{id}', 'ArticlesController#update')->name('articles.update');
in controller
public function update(Request $request, $id)
{
// logic
}
don't forget to use Request in controller
in blade
it's better to use naming for routes but it might be a problem in your action
<form method="POST" action="{{ route('articles.update', $article->id) }}">
simple way using blade
<form action="/articles/{{$article->id}}" method="POST">
#method('put')
#csrf
</form>

Redirect() Doesnt work after POST Submit with #csrf, keeps on reloading the page

i just started laravel and i am trying to create a simple post form for practive, when submitted it gives an 419 Error so I used #csrf inside the form.
HTML Form:
<form method="POST" action="/posts">
#csrf
<input type="text" name="title" placeholder="Enter Title">
<input type="submit" name="submit">
</form>
Route:
Route::resource('/posts', 'PostController');
Store Function:
public function store(Request $request)
{
//
$post = new Post;
...
$post->save();
return redirect('/posts'); //Tried
return redirect()->route('posts.index');
}
#csrf fixed the 419 error and made the post work.
The Post works only when there is no redirect(). I think the #csrf is making the redirect() not work.
I've been searching and couldn't find a solution. How do I fix this?
make action="{{ route('posts.store') }}" instead of action="/posts"
make return redirect()->route('posts.index');

Laravel route post vs get

project is laravel 5.6. My project has 2 routes:
web.php
Route::get('testa', 'HomeController#showTestForm')->name('test');
Route::post('testa', 'HomeController#doTest');
HomeController :
public function showTestForm()
{
Log::warning('from showTestForm');
return view('test');
}
.public function doTest(Request $request)
{
Log::info('from doTest');
// return Input::all();
return view('test', [
'input' => implode(', ', Input::all()),
]);
}
test.blade.php
<form method="post" action="{{ route('test') }}">
#csrf
<input type="text" name="inputvalue">
<button type="submit" class="btn btn-primary">
merge
</button>
</form>
<div>Result</div>
#if(isset($input))
{{$input}}
#endif
Why is working post on route('test') ?
Thank you.
The reason that route('test') works even though your form is a POST request is because route() is just a helper function to generate a url and your GET and POST routes both use the same url.
You've specified in your form to make a post request and it's going to send it to the url provided (which will be the same as your GET request in this case).
Edit:
Route::post('testa', 'HomeController#doTest')->name('postTest');
and then use
<form method="post" action="{{ route('postTest') }}">
Hope this will work
It is because you'are calling the wrong route.
Change :
<form method="post" action="{{ route('test') }}">
to :
<form method="post" action="{{ url('/testa') }}">
or follow the previous answer steps (name the post route then call it)
I believe from the OP they are not understanding how the same route can accept both a GET request and a POST request.
The difference is in how the server receives the data.
Have a read: HTTP Requests

Error while editing a post using Laravel update

This is my html form
<form class="form-horizontal" action="{{action('BlogController#update',[$blog->id]) }}" method="post">
<input name="method" type="hidden" value="patch"/>
<div class="form-group">
<input name="_token" type="hidden" value="{{ csrf_token() }}"/>
Here is route:
Route::patch('blog/{id}','BlogController#update');
Controller :
public function update(Request $request,$id){
$input = $request->all();
$blog =findOrFail($id);
Blog::update($input);
//var_dump($input);
return back();
}
Can you please show me where is the issue?
In your code you have write $blog = findOrFail($id); to get blog which is not correct. You can do it using
$blog = Blog::findOrFail($id);
Now you have the blog, you need to update the blog. So, the update code should be
$blog->update($input);
To make this update method works, you need to make the fields(the fields you are updating) fillable in Blog model.
You're using the wrong syntax. Do something like this to make it work:
public function update(Request $request, $id)
{
Blog::where('id', $id)->update($request->all());
return back();
}
give the name whatever you wish say blog:
Route::patch('blog/{id}','BlogController#update')->name('blog');
your HTML code
<form class="form-horizontal" action="{{route('blog', $blog->id)}}" method="post">
hope this help you!!
you have many syntax problems!
try it this way:
form:
<form class="form-horizontal"
action="{{ route('blog.update', ['id' => $blog->id]) }}"
method="post">
{{ csrf_field() }}
<input name="_method" type="hidden" value="patch"/>
<!-- other inputs -->
</form>
Route:
Route::any('blog/{id}','BlogController#update')->name('blog.update');
Controller:
public function update(Request $request, $id){
$blog = Blog::findOrFail($id);
$blog->update([
'key' => 'value'
]);
// never use $request->all() because of security issues!
return back();
}
<form class="form-horizontal" action="{{route('blog.update',[$blog->id]) }}" method="post">
{{csrf_field()}}
{{ method_field('PATCH') }}
Your Route Like This
Route::resource('blog', 'BlogController');
Your Controller
public function update(Request $request,$id){
$blog =Blog::findOrFail($id);
$blog->database_fieldname1=$request->value1;
$blog->database_fieldname2=$request->value2;
$blog->database_fieldname3=$request->value3;
$blog->save();
return back();
}

MethodNotAllowedHttpException in RouteCollection.php line 218

i'm new to laravel, and i found few decent tutorials to help me understand and get started with it.
the problem is-> whenever i want to use the post method this exception raises MethodNotAllowedHttpExceptionbut unlike, maybe 99% of who asked similar questions, in my case it says the exception is in RouteCollection.php line 218, which is unusual but not to laravel 5.2.x
the following is the methode post in routes.php:
Route::post('/ajouter_produit',
[
'uses'=>'ProductController#addProduct',
'as'=>'ajouter_produit',
]);
i even tried adding this method to a middleware route group but the problem remained.
this is my controller:
public function addProduct (Request $request)
{
$this->validate($request, [
'label'=>'required|alpha',
'prix'=>'required|numeric',
]);
$prod = new Product();
$prod->label=$request['label'];
$prod->type=$request['type'];
$prod->prix=$request['prix'];
$prod->save();
return view('welcome');
}
and this is my form:
<form action="{{ route('ajouter_produit') }}" method="post" >
<input type="text" name="label" id="label"/>
<select name="type" id="type">
<option value="1">Par unité</option>
<option value="2" selected>Par kilo</option>
</select>
<input type="text" name="prix" id="prix"/>
<button type="submit">Ajouter</button>
<input type="hidden" value="{{ Session::token() }}" name="_token"/>
i also tried this but it raised the same problem:
Route::post('/trypost', function () {
return 'hello post';
});
can you please help me !!
if you need any other source just ask for it.
Every effort will be much appreciated. thank you
take note that if you are using route(), it is expecting route name, such as user.store or user.update.
so my suggestion is, try use url() for your open form
<form action="{{ url('ajouter_produit') }}" method="post" >
more details on laravel docs
"#mydo47: Missing method get. First you should create route with method get return view. Next, in view page you call method post validate and save to your model." this solved it

Categories