当前位置: 首页 > 知识库问答 >
问题:

方法App\Http\Controllers\Auth\RegisterController::validator不存在

督阿苏
2023-03-14

我正在尝试在我的注册控制器中注册多用户,并使用以下代码进行检查。但它告诉我一个错误:

方法App\Http\Controllers\Auth\RegisterController::validator不存在。

<?php

namespace App\Http\Controllers\Auth;

use App\Admin;
use App\Manager;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
use Illuminate\Foundation\Auth\RegistersUsers;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Http\Request;


class RegisterController extends Controller
{
    use RegistersUsers;

    public function __construct()
    {
        $this->middleware('guest');
        $this->middleware('guest:admin');
        $this->middleware('guest:manager');
    }

    protected function createAdmin(Request $request)
    {
        $this->validator($request->all())->validate();
        $admin = Admin::create([
            'name' => $request['name'],
            'email' => $request['email'],
            'password' => Hash::make($request['password']),
        ]);
        return redirect()->intended('login/admin');
    }

    protected function createManager(Request $request)
    {
        $this->validator($request->all())->validate();
        $manager = Manager::create([
            'name' => $request['name'],
            'email' => $request['email'],
            'password' => Hash::make($request['password']),
        ]);
        return redirect()->intended('login/manager');
    }
}

共有1个答案

龚迪
2023-03-14

尝试添加validator()函数,如下所示:

protected function validator(array $data, $table)
{
    return Validator::make($data, [
        'name' => ['required', 'string', 'max:255'],
        'email' => ['required', 'string', 'email', 'max:255', 'unique:'.$table],
        'password' => ['required', 'string', 'min:8', 'confirmed'],
    ]);
}

然后您可以这样进行验证:

$this->validator($request->all(), 'table_name')->validate();

用相应的名称更改表名称。

 类似资料: