skip to Main Content

The problem arises when I use the "save" method to save the data, as it returns as undefined, this is the code :

public function profile()
{
    return view('profile.edit', ['user' => Auth::user()]);
}

public function update(Request $request)
{
    $request->validate([
        'fullName' => 'required|string|max:255',
        'email' => 'required|string|email|max:255|unique:users,email,' . Auth::id(),
        'image' => 'nullable|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
    ]);

    $user = Auth::user();
    $user->name = $request->input('fullName');
    $user->email = $request->input('email');

    if ($request->hasFile('image')) {
        if ($user->image) {
            Storage::delete('public/profile_images/' . $user->image);
        }
        $imageName = time() . '.' . $request->image->extension();
        $request->image->storeAs('public/profile_images', $imageName);
        $user->image = $imageName;
    }

    $user->save(); // it gives me undefined

    return redirect()->route('profile.edit')->with('success', 'Profile updated successfully.');
}

public function updatePassword(Request $request)
{
    $request->validate([
        'currentPassword' => 'required',
        'newPassword' => 'required|string|min:8|confirmed',
    ]);

    $user = Auth::user();

    if (!Hash::check($request->input('currentPassword'), $user->password)) {
        return back()->withErrors(['currentPassword' => 'Current password is incorrect']);
    }

    $user->password = Hash::make($request->input('newPassword'));
    $user->save(); // it gives me undefined

    return redirect()->route('profile.edit')->with('success', 'Password changed successfully.');
}
  • I make sure that I’m using the Auth::user() object correctly.
  • Ensure that the form correctly sends data to the appropriate controller and that the image field is configured properly for file uploads.
  • Ensure that the User model utilizes Eloquent traits correctly and that the configuration is accurate.

2

Answers


  1. This statement // $user = Auth::user(); // is bad.

    So suggest that statement.

    use AppModelsUser;

    $user = new User();

    Login or Signup to reply.
  2. Check if this path were included in your controller

    use IlluminateSupportFacadesAuth;
    

    Try dd($user) to see your user data

    Or you might as well update the data using your email since it’s unique value;

    $user = User::where('email', $request->email)->first();
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search