Home > Enterprise >  How do I ignore inputs that don't have data in update method
How do I ignore inputs that don't have data in update method

Time:10-14

i have a form to update user data name and email and username the qustion is if user want to update only user how i keep same email and name or change only name in my case the valdetion required all inputs this is my controller

 public function update(Request $request, $username )
{
    if($username != Auth::user()->username){
        return abort('401');
    }

    $request->validate([

        'name' => 'min:10|max:50|required',
        'username' => 'unique:users|max:30|min:3|required',
        'email' => 'email|unique:users|max:200|required',

    ]);
    $checkuserindb = User::findOrFail(Auth::user()->id);

        $checkuserindb->update([
            'name' => $request->name,
            'username' => $request->username,
            'email' => $request->email
        ]);
        return redirect()->route('userProfile', $request->username)->with('message', 'your account has been updated!');



}

CodePudding user response:

On your input field just add the actual value from database like this:

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

Also use 'sometimes' rule and update any of the fields you want.

  • Related