0

Hello I have just started in Laravel and am trying to update the users profile: My route:

Route::patch('users/{user}/update',  ['as' => 'users.update', 'uses' => 'UserController@update']);

My View:

<form method="post" action="{{route('users.update', $user)}}">

    {{ csrf_field() }}
    {{ method_field('patch') }}

    <input type="text" name="name"  value="{{ $user->name }}" />
    <input type="email" name="email"  value="{{ $user->email }}" />


    <input type="password" name="password" />

    <input type="password" name="password_confirmation" />

    <button type="submit">Send</button>
</form>

and my update function in UserController:

public function update(User $user)
    { 

        $this->validate(request(), [
            'name' => 'required',
            'email' => 'required|email|unique:users',
        ]);

        $user->name = Request::input('name');
        $user->email = Request::input('email');

        $user->save();
        Flash::message('Your account has been updated!');
        return back();
    }

I dont get any errors yet my user profiles aren't updated.Can sb help me?

5
  • First try adding some dd() to action to see what is going on - dump request data and user to see if you get correct data. Commented Apr 1, 2018 at 18:26
  • @Bostjan I just did the dd($user) in controller and it returns code #original: array:10 among others Commented Apr 1, 2018 at 18:29
  • What about dd(Request::all()) to see if submited values are ok? Commented Apr 1, 2018 at 18:39
  • @Bostjan I think the error is in the ` $this->validate(request(), [ ` Commented Apr 1, 2018 at 18:39
  • @Bostjan Non-static method Illuminate\Http\Request::all() should not be called statically...if I call it after $this...it gives no error and no output Commented Apr 1, 2018 at 18:42

1 Answer 1

2

I think fields aren't updated because validations fails. If you check your form you have four (4) fields with name 'email'. With this it fails validator for email.

You can try displaying errors in blade file: https://laravel.com/docs/5.6/validation#quick-displaying-the-validation-errors

And I suggest to inject request as method parameters. Like this;

public function update(User $user, Request $request)
{ 
    $data = $request->validate([
        'name' => 'required',
        'email' => 'required|email|unique:users',
    ]);

    $user->fill($data);
    $user->save();
    Flash::message('Your account has been updated!');
    return back();
}
Sign up to request clarification or add additional context in comments.

3 Comments

The email must be a valid email address.P.S the email is a valid email address
Did you fix your form? You have 4 email fields
thanks it seems that the error has been the email fields

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.