Search code examples
phplaravellaravel-jetstream

how to create new user from custom controller in laravel Jetstream


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?


Solution

  • It is because you never hash the password, one way of solving this would be to add before the User::create the following line:

    $validatedData['password'] = Hash::make($validatedData['password']);