Mustafa UYSAL
Mustafa UYSAL

Reputation: 143

Laravel Routing Post method failed

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

Answers (6)

hkucuk
hkucuk

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

Kabiru Wahab
Kabiru Wahab

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

Don
Don

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

Therichpost
Therichpost

Reputation: 1815

Did you use below code into your form:

{!! Form::token() !!}

or you can try use "put" instead of "get"

Upvotes: 0

Ralph519
Ralph519

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

AddWeb Solution Pvt Ltd
AddWeb Solution Pvt Ltd

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

Related Questions