jeudi 1 décembre 2016

Update user profile in Laravel 5.1

I am trying to update some user profile data as follows:

To update profile in routes.php

Route::get('/profile/update/{id}', [
        'as' => 'user.edit',
        'uses' => 'UserController@edit'
    ]);

In my UserController.php

public function edit($id)
{
      $user = User::findOrFail($id);

      return view('user/edit', compact('user'));
}

My action in edit.blade.php

{!! Form::model($user, ['action' => ['UserController@update', 'id' => $user->id], 'method' => 'PUT']) !!}
                    <div class="form-group">
                        {!! Form::label('name', 'Name') !!}
                        {!! Form::text('name', $user->name, [
                            'class' => 'form-control'
                        ]) !!}
                    </div>

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

                    <div class="form-group">
                        {!! Form::label('phone', 'Phone') !!}
                        {!! Form::text('phone', $user->phone, [
                            'class' => 'form-control'
                        ]) !!}
                    </div>

                    <p><button type="submit" class="btn btn-primary">Update Profile</button></p>
                {!! Form::close() !!}

The route for update:

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

And the function in the UserController.php

public function update(Request $request, $id)
    {
        $user = User::findOrFail($id);
        $user->update([
            $request->input('name'),
            $request->input('email'),
            $request->input('phone')
        ]);

        return Redirect::route('user.profile');
    }

After updating the field I want (for example the phone), it simply returns to the profile screen, but does not update it in the database. What am I doing wrong? Thank you very much for your help.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire