如何在请求验证器中哈希密码?

问题描述 投票:0回答:1

我想对密码进行哈希处理并将其填写到我的请求验证器中,以便能够保存新员工注册。您能帮助如何在请求验证器中散列密码吗?

这是我的项目的详细信息:

型号:

<?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');
    }
 }

请求验证器:

<?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'],
        ];
    }
 }

控制器:

<?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');
    }
}

查看表格:

@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
laravel eloquent hash request laravel-10
1个回答
0
投票

如果您只需将

hashed
转换添加到
password
列,则无需对密码进行哈希处理,就像默认的
User
模型一样:

https://github.com/laravel/laravel/blob/c12fd185e64b6fd652243e06f290f438164ddde5/app/Models/User.php#L44

您的模型类还应该使用 StudlyCase 和单数命名,因此应该是

Employee
而不是“employes”。遵循 Laravel 的命名约定,您就不需要编写那么多代码。

© www.soinside.com 2019 - 2024. All rights reserved.