Support the ongoing development of Laravel.io →
Input Forms Validation

I've recently just started with Laravel, and loving it - made this to update a user's data - is it too large? Am I doing it wrong? :)

/**
 * Update the specified resource in storage.
 *
 * @param  int  $id
 * @return Response
 */
public function update($id, UserEditRequest $request)
{
	$user = User::findOrFail($id);
    $passwordstatus = Hash::check($request->password, $user->password);
    $status = [
        'name' => false,
        'email' => false,
        'password' => false,
    ];

    if(!$passwordstatus)
    {
        return redirect()->route('user.edit', ['user' => $user->id])->with('error_message', 'Your password was entered incorrectly.');
    }

    /* Start name change */
    $name = $request->name;
    if(!empty($name) && $name != $user->name)
    {
        // Request isn't empty, and name isn't current name
        $user->name = $request->name;
        $status['name'] = true;
    }
    /* End name change */

    /* Start email change */
    $email = $request->email;
    if(!empty($email) && $email != $user->email)
    {
        $user->email = $request->email;
        $status['email'] = true;
    }
    /*End email change */

    /* Start password change */
    $new_password = $request->new_password;
    $newpasswordcheck = Hash::check($new_password, $user->password);
    if(!empty($new_password) && !$newpasswordcheck)
    {
        $user->password = Hash::make($new_password);
        $status['password'] = true;
    }
    /* Finish password change */
    $user->save();

    $template = "{$name}, we have successfully updated your ";
    $message = $template;
    $count = 0;
    foreach($status as $status_key => $status_value)
    {
        if($status_value)
        {
            if($count == 0)
            {
                $message.= $status_key;
            }else{
                $message .= ", ".$status_key;
            }
            $count++;
        }
    }

    if($status['password'])
    {
        $message.= ". Please log in with your new credentials.";
        \Auth::logout();
        return redirect('auth/login')->with('success_message', $message);
    }

    if($message == $template)
    {
        return redirect('/')->with('success_message', 'Updated nothing.');
    }

    return redirect('/')->with('success_message', $message);

}

In case you were wondering, none of the fields apart from my 'password' field is required, to make the user's experience easier :)

Last updated 3 years ago.
0

You could probably simplify it by using getDirty method to get list of changed attributes. And, optionally, move password check to validation in UserEditRequest.

0

Xum said:

You could probably simplify it by using getDirty method to get list of changed attributes. And, optionally, move password check to validation in UserEditRequest.

How would I go about validating a password in the validation method?

0

you could split up the statements in to more manageable functions and call them one by one. obviously you will need to do the same tasks from the different controller function in the future. and there's too much extra line feeds in your code. i dont see why you need them.

0

Sign in to participate in this thread!

Eventy

Your banner here too?

muyiwaolu muyiwaolu Joined 24 Aug 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.