Reputation: 143
This is my html form;
<form action="{{url('admin/users/update/'.$user->id)}}" method="post">
{{csrf_field()}}
{{method_field('put')}}
and this is my router;
Route::post('users/update/{id}', 'UsersController@update');
and this is my controller
public function update($id)
{
//$id=$_POST['id'];
$user = \App\User::find($id);
$user->email = $_POST['email'];
$user->name = $_POST['name'];
if ($_POST['password'] != '') {
$user->password = Hash::make($_POST['password']);
}
$user->user_level = $_POST['user_level'];
$user->location =$_POST['location'];
$user->gender = $_POST['gender'];
$user->save();
My code doesn't work. How can I fix it? The error is Symphony\Component\HttpKernel\Exception\MethodNotAllowedHttpException No Message
Upvotes: 3
Views: 1957
Reputation: 345
You need to remove {{method_field ('put')}}. Because this code means that your form is being sent via put method and there is no put in the router.
Solution 1. Delete {{method_field ('put')}}.
Solution 2. Change the router definition to:
Route::put('users/update/{id}', 'UsersController@update');
Upvotes: 1
Reputation: 87
I suggest you should install Laravel Collection it helps in adding some token to the form Check out this documentation here https://laravelcollective.com/docs/5.2/html Please notify me if it works.
Upvotes: 0
Reputation: 1
did you declare @csrf in the form.
use App\User;
use Auth;
use Illuminate\Http\Request;
public function update(Request $r,$id)
{
//$id=$_POST['id'];
$user = User::find($id); // if auth is necessary $user = Auth::id();
$user->update([
'email' => $r->email,
'name' => $r->name,
'password' => Hash::make($r['password']),
'user_level' => $r->user_level,
'location'=> $r->location,
'gender' => $r->gender
]);
}
in your web.php
Route::post('/user/update/{id}', [
'uses' => 'UsersController@update',
'as' => 'user.store'
]);
in your form
<form class="" action="{{route('user.store')}}" method="post">
@csrf
Upvotes: 0
Reputation: 1815
Did you use below code into your form:
{!! Form::token() !!}
or you can try use "put" instead of "get"
Upvotes: 0
Reputation: 95
Try using the HTTP request via dependency injection
Declare first the Illuminate\Http\Request class
use Illuminate\Http\Request;
public function update(Request $request, $id)
{
//$id=$_POST['id'];
$user = \App\User::find($id);
$user->email = $request->input('email');
$user->name = $request->input('name');
if ($request->input('password') != '') {
$user->password = Hash::make($request->input('password'));
}
$user->user_level =$request->input('userlevel');
$user->location =$request->input('location');
$user->gender = $request->input('gender');
$user->save();
}
Upvotes: 0
Reputation: 21681
You should try this:
Route:
Route::post('users/update/{id}', 'UsersController@update')->name('user.update);
Form:
<form action="{{['route' => ['user.update', $user->id]}}" method="post">
Upvotes: 0