My data is not updated of user table

  Thu 08 / 02 / 2024

  Posted by: Vanita saini

Category : Laravel

Tags : laravel , php , php laravel

Description

I just want to update user information like its role but it's not updated. When we save successful message showing but record not updated



View Code
                        public function update(Request $request, User $user)
    {
        try{
            //dd($request);
            DB::beginTransaction();

            // Validate the incoming request data
            $validator = Validator::make($request->all(), [
                'name' => 'required|string|max:255',
                'email' => 'required|string|email|max:255|unique:users,email,' .$user->id,
                //'password' => ['required', 'string', 'min:8', 'confirmed'],
            ]);

            if ($validator->fails()) {
                return redirect()->back()->withErrors($validator)->withInput();
            } else {
                //dd($user);
                // Update user information
                $user->update([
                    'name' => $request->name,
                    'email' => $request->email,
                    'is_admin' => $request->is_admin
                ]);

                session()->flash('alert-success', 'User data updated successfully');
                return to_route('users.index');
            }

            DB::commit();

        }
        catch(\Exception $ex){
            DB::rollback();
            dd($ex->getMessage());
        }
    }
                
  Answered by CDL

@vanita you added beginTransaction which is used to make sure everything inside it should be executed successfully, otherwise if something fails, it will roll back the data, so in your case, your user is updating but DBTransaction is rolling back this data.


How you can prevent it from?

Use transactions like this

~~~

DB::beginTransaction();


 $user->update([

                    'name' => $request->name,

                    'email' => $request->email,

                    'is_admin' => $request->is_admin

                ]);


DB::commit();

~~~

Solved
  Comment   Share
0 Likes   1 Comment


comment-author
Vanita saini Author

Thank you sir, mein 2 days se lgi hue the es issue mein. thank u so much.



Reply


commented 8 months ago
comment-author
Hadayat Niazi Super Admin

You are welcome @vanita