1

I'm using Laravel 5.4 and I have some problems with my update function in my UsersController. I always get a "MethodNotAllowedHttpException". I already figured out that it could happen, when your form is e.g. post, but the routing is get - that shouldn't be the case here I guess...

I'll just give you my code snippets, maybe you can already see it there. Just so you know: I'm completely new to Laravel!

users.blade.php

@extends('layouts.app')

@section('content')


{!! Form::model($user, ['method' => 'PATCH', 'route' => ['users.update',    $user->id]]) !!}

<div class="form-group">
{!! Form::label('email', 'E-Mail:', ['class' => 'control-label']) !!}
{!! Form::text('email', null, ['class' => 'form-control']) !!}
</div>

<div class="form-group">
{!! Form::label('old_password', 'Old assword:', ['class' => 'control- label']) !!}
{!! Form::password('old_password',['class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('password', 'New password:', ['class' => 'control-label']) !!}
{!! Form::password( 'password', ['class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('password', 'Repeat new password:', ['class' => 'control-label']) !!}
{!! Form::password( 'password_confirmation', ['class' => 'form-control']) !!}
</div>

{!! Form::submit('Update', ['class' => 'btn btn-primary']) !!}

{!! Form::close() !!}

{!! Form::model($user, ['method' => 'DELETE', 'route' => ['users.destroy',     $user->id]]) !!}

{!! Form::submit('Delete', ['class' => 'btn btn-danger']) !!}
{!! Form::close() !!}

@stop

routing via web.php

Route::resource('users', 'UsersController');

update in UsersController.php

public function update(Request $request, $id)
{
// $user = Auth::user();
$user = User::findOrFail($id);

$this->validate($request, [
  'email' => 'required|string|email|max:255|unique:users',
  'old_password' => 'nullable|string|min:6|confirmed',
  'password' => 'nullable|string|min:6|confirmed',
  'password_comfirmation' => 'nullable|string|min:6|confirmed',
]);

$old_password = $request->old_password;
$password = $request->password;
$password_confirmation->$request->password_confirmation;


if (Hash::check($old_password, $hashedPassword)) {
  if($password == $password_confirmation){

    $user->email = $request->email;
    $user->password = Hash::make($password);
    $user->update();


  }
  else {
    Session::flash('flash_message', 'Password Confirmation not equal!');
    return redirect()->back();
  }
}
else {
  Session::flash('flash_message', 'Old password wrong!');
  return redirect()->back();
}


Session::flash('flash_message', 'User successfully added!');

return redirect()->back();
}

When I do php artisan route:list, it tells me:

    | GET|HEAD  | users/create           | users.create     |   App\Http\Controllers\UsersController@create                            | web          |
    | PUT|PATCH | users/{user}           | users.update     | App\Http\Controllers\UsersController@update                            | web          |
    | GET|HEAD  | users/{user}           | users.show       | App\Http\Controllers\UsersController@show                              | web          |
    | DELETE    | users/{user}           | users.destroy    | App\Http\Controllers\UsersController@destroy                           | web          |
    | GET|HEAD  | users/{user}/edit      | users.edit       | App\Http\Controllers\UsersController@edit  

I didn't really try to update function yet, because I don't even get to that point.... Do you have any hints?

----EDIT----

This is the error: It looks as if it doesn't allow POST - but I don't even use POST. I tried the POST option in the comments as well, but I still get the same error...

I have a destroy function for users that works exactly the same (but method: DELETE) and it doesn't throw any exception and everything works well.

Thanks a lot! :) Nadine

Nadine S.
  • 11
  • 3
  • The problem may be due to using `PATCH`. Look at this: https://stackoverflow.com/questions/25857650/laravel-form-wont-patch-only-post-nested-restfull-controllers-methodnotallo – ayip Jun 12 '17 at 14:25
  • Can you paste your whole routes file, if I update I do it this way: `Route::post('update', ['as' => 'update', 'uses' => 'DanceController@update']);` – utdev Jun 12 '17 at 15:04
  • add `{!! method_field('patch') !!}` instead of method in `{!! Form` tag. – Bhaumik Pandhi Jun 12 '17 at 15:17
  • I tried all of your ideas, but unfortunately I still get the same error... I added a picture with the error I get, maybe you have further ideas, when you see it! :) – Nadine S. Jun 13 '17 at 07:39

1 Answers1

0

Use the method PUT instead of PATCH

{!! Form::model($user, ['method' => 'PUT', 'route' => ['users.update',    $user->id]]) !!}
Vishal Varshney
  • 905
  • 6
  • 11