John Davidson

php - how to create new user from custom controller in laravel Jetstream

0 comments
Message:


I am working on custom controller that will allow the admin of the system to create new user


so far i did the below for store function:


public function store(Request $request)
{
$validatedData = $request->validate([
'name' => 'required|max:255',
'email' => 'required',
'password' => 'required',
'role' => 'required',
'department' => 'required',
]);
$user = User::create($validatedData);

return redirect('add-staff')->with('success', 'User is successfully saved');
}

the above function will create new user in users database but i can't login with the new created user and it will show un-hashed password in the databse


is there a way to create new user with jetstream from custom controller?



My Answer:
>validate([_ 'name' => 'required|string|max:255',_ 'email' => 'required|string|email|max:255|unique:users',_ 'password' => 'required|string|confirmed',_ ]);_ $user = User::create([_ 'name' => $validatedData['name'],_ 'email' => $validatedData['email'],_ 'password' => Hash::make($validatedData['password']),_ ]);_ return response()->json(['message' => 'User created successfully', 'user' => $user], 201);_}

Rate this post

5 of 5 based on 7581 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us