Update User by id can't work did i do wrong?

Viewed 26

Update User by id can't work did i do wrong?

this is my Form :

 <form method="post" action="/dashboard/crud/{{ $user->id }}" class="mb-5">
                  <input type="hidden" name="_method" value="PUT">                    
                  @csrf

                    <div class="mb-3">
                      <label for="name" class="form-label">Nama Lengkap</label>
                      <input type="text" class="form-control"@error('name') is-invalid @enderror id="name" name="name" required autofocus value="{{ old('name', $user->name) }}">
                    </div>


                      <div class="mb-3">
                        <label for="email" class="form-label">Email</label>
                        <input type="email" class="form-control"@error('email') is-invalid @enderror id="email" name="email" required autofocus value="{{ old('email', $user->email) }}">
                      </div>

                    <button type="submit" class="btn btn-primary">Edit Akun</button>
                </form>

this is my Route:

Route::put("/dashboard/crud/{user}",[DashboardUserController::class,'update'])->middleware('auth');

this is my Controller

 public function update(Request $request, User $user)
    {
        $rules =[
            'name' => 'required|max:255',
            'email' => 'required|email:dns|unique:users',
        ];

        $validatedData = $request->validate($rules);

        User::where('id', $user->id)
        -> update($validatedData);

        return redirect('/dashboard/crud')->with('success','data telah terupdate') ;
        
    }

i want to update user by id please help... my code doesn't work

1 Answers

Just replace your code with this, // your code

User::where('id', $user->id)
    -> update($validatedData);

//replace this with below one.

$userData=[
'name'=>$request->name,
'email'=>$request->email,
];
//name and email key from $userData should match with your user table column.
User::where('id', $user->id)->update($userData);
Related