laravel - update user profile


Laravel - Update User Profile


If you want to update users Name and Email

  • Create new controller, name it profileController
        public function index()
        {
            return view('usersprofile.index');
        }
    
        public function update(Request $request, $id) {
            $user = User::findOrFail($id);
            $user->name = $request->get('name');
            $user->email = $request->get('email');
            $user->save();
            return redirect('users')->with('status', 'Profile updated!');
        }
  • Create folder usersProfile inside resource/view
    
    <form method="POST" action="{{ route('users.update', Auth::user()->id) }}">
        <input type="hidden" name="_token" value="{{ csrf_token() }}">
        <input type="hidden" name="_method" value="PUT">
        <div class="form-group">
            <label for="name">Name</label>
            <input type="text" name="name" value="{{ Auth::user()->name }}" class="form-control">
        </div>
        <div class="form-group">
            <label for="email">Email</label>
            <input type="email" name="email" value="{{ Auth::user()->email }}" class="form-control">
        </div>
        <button type="submit" class="btn btn-primary">
            <i class="fa fa-btn fa-sign-in"></i>Update
        </button>
    </form>
  • Your Route will be like this:
    
    Route::resource('users', 'YourController');
    
  • add Use App\User;


Thank you