I want to hash password and fill it in my request validator to be able to save a new employee registration. Can you help how to hash password in request validator?
Here details of my project:
Model:
<?php
namespace AppModels;
use IlluminateDatabaseEloquentFactoriesHasFactory;
use IlluminateDatabaseEloquentModel;
use IlluminateSupportFacadesHash;
class employes extends Model
{
use HasFactory;
protected $fillable = [
'employee_name',
'login',
'password',
'start_date',
'post',
'team_number',
'isAdmin',
];
public function orders()
{
return $this->hasMany(order::class,foreignKey:'employes_id',localKey:'id');
}
}
Request validator:
<?php
namespace AppHttpRequests;
use IlluminateFoundationHttpFormRequest;
class AddEmployeeRequest extends FormRequest
{
public function authorize(): bool
{
return true;
}
public function rules(): array
{
return [
'employee_name' => ['required','string','min:4'],
'login' => ['required','email','min:4'],
'password' => ['required','string','min:4'],
'start_date' => ['required','date'],
'post' => ['required','string','min:2'],
'team_number' => ['required','integer','max:4'],
'isAdmin' => ['required','boolean'],
];
}
}
Controller:
<?php
namespace AppHttpControllers;
use AppHttpRequestsAddEmployeeRequest;
use AppModelsemployes;
use IlluminateHttpRequest;
class EmployeController extends Controller
{
public function store(AddEmployeeRequest $request)
{
$request->validated();
$employee = $request->validated();
employes::create($employee);
return to_route('admin.employes')->with('success', 'The employee is saved successfully');
}
}
View form:
@extends('layout')
@section('title','Add new Employee')
@section('content')
<div class="container">
<form action="{{ route('admin.store_employee') }}" method="post" class="vstack gap-2">
@csrf
<div class="row">
@include('shared.input',['class'=>'col','type'=>'text','name'=>'employee_name'])
@include('shared.input',['class'=>'col','type'=>'text','name'=>'login'])
@include('shared.input',['class'=>'col','type'=>'password','name'=>'Password'])
@include('shared.input',['class'=>'col','type'=>'date','name'=>'start_date'])
</div>
<div class="row">
@include('shared.input',['class'=>'col','type'=>'text','name'=>'post'])
@include('shared.input',['class'=>'col','type'=>'integer','name'=>'team_number'])
</div>
<div class="row">
<label for="isAdmin">User Status</label>
<select value="isAdmin" class="col form-control rounded-0" name="isAdmin" id="isAdmin">
<option selected value="">Choose user type</option>
<option value="1">Administrator</option>
<option value="0">User</option>
</select>
@error('isAdmin')
<div class="text-danger">
{{ $message }}
</div>
@enderror
</div>
<button class="btn btn-primary">Add</button>
</form>
</div>
@endsection
2
Answers
You don’t need to hash the password if you just add the
hashed
cast to yourpassword
column, like the defaultUser
model does:https://github.com/laravel/laravel/blob/c12fd185e64b6fd652243e06f290f438164ddde5/app/Models/User.php#L44
Your model class should also be named using StudlyCase and singular, so should be
Employee
and not “employes”. Stick to Laravel’s naming conventions and you won’t need to write as much code.If you mean hashing the password before storing it in the database, here is your modified version of your code: