Skip to content

Larave noting save hash password in database

My need create new user in admin dashboard, this store function, but database saving string not hash, please help. When I output via dd(), then the hash working

`
public function store(Request $request)
{
   $data = $request->validate([
        'name' => 'required|string',
        'email' => 'required|email|unique:users',
        'password' => 'required|string|min:8|confirmed'
    ]);

    $object = new Specialist();
    $object->groups = 3;
    $object->password = Hash::make($data['password']);
    $object->fill(request()->all());

    $object->save();

    return redirect()->route('specialists.index');
}
`

Model

`class Specialist extends Model
{
  // USE DATABASE TABLE users
  protected $table = 'users';

  // FILL COLUMNS...
  protected $fillable = ['email', 'password', 'name'];

}`

Advertisement

Answer

$object->fill(request()->all());

This line is overwriting the password field because request()->all() includes password.

Use except() method to remove the fields that you don’t need:

 $object->password = Hash::make($data['password']);
 $object->fill(request()->except('password'));
User contributions licensed under: CC BY-SA
8 People found this is helpful