Search code examples
laraveleloquenthashrequestlaravel-10

How to hash password in request validator?


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 App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Hash;

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 App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

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 App\Http\Controllers;

use App\Http\Requests\AddEmployeeRequest;
use App\Models\employes;
use Illuminate\Http\Request;

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

Solution

  • You don’t need to hash the password if you just add the hashed cast to your password column, like the default User 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.