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

传递给validator()的Laravel参数1必须是给定数组的Illumb\Http\Request实例

司徒阳曜
2023-03-14

我在laravel上有一个注册和登录系统,没有电子邮件验证。今天,我想使用以下指南添加电子邮件验证:https://mailtrap.io/blog/laravel-email-verification/完成所有操作后,我尝试注册一个用户,但出现了错误“传递到App\Http\Controllers\Auth\RegisterController::validator()的参数1必须是在第32行的/var/www/sinemori/vendor/laravel/ui/Auth backend/RegistersUsers.php中调用的light\Http\Request、给定数组的实例”我在谷歌上搜索了这个错误,大多数答案都是关于use\Http\Request的;我已经用过了。

在我的网上。我正在使用的php

Auth::routes(['verify' => true]);
Auth::routes();

申请/登记路线

这是我的注册控制器

<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use App\Models\User;
use Illuminate\Foundation\Auth\RegistersUsers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;

class RegisterController extends Controller
{
use RegistersUsers;
public function __construct()
{
    $this->middleware('guest');
}

protected function validator(Request $request)
{
    $this->validate($request, ['name' => 'required|string|min:2|max:255',
        'surname' => 'required|string|min:2|max:255',
        'email' => 'required|string|email|max:255|unique:users',
        'username' => 'required|string|max:255|unique:users',
        'password' => 'required|string|min:8|confirmed',
        'password_confirmation' => 'required']);
}

public function store(Request $request){
    $this->validator($request);

    User::create([
        'name' => $request->name,
        'surname' => $request->surname,
        'email' => $request->email,
        'username' => $request->username,
        'password' => Hash::make($request->password),
        'profile_img' => "/images/users/default_profile_picture.png"
    ]);

    return redirect()->intended('/login');
}
}

这是登记簿。刀身php

@if ($errors->any())
<div class="alert alert-danger">
    <ul>
        @foreach ($errors->all() as $error)
            <li>{{ $error }}</li>
        @endforeach
    </ul>
</div>
@endif
<form method="post" action="{{ route('register') }}">
@csrf
<div class="form-group">
    <label>Name</label>
    <input type="text" name="name" class="form-control p_input">
</div>
<div class="form-group">
    <label>Surname</label>
    <input type="text" name="surname" class="form-control p_input">
</div>
<div class="form-group">
    <label>Email</label>
    <input type="email" name="email" class="form-control p_input">
</div>
<div class="form-group">
    <label>Username</label>
    <input type="text" name="username" class="form-control p_input">
</div>
<div class="form-group">
    <label>Password</label>
    <input type="password" name="password" class="form-control p_input">
</div>
<div class="form-group">
    <label>Password Again</label>
    <input type="password" name="password_confirmation" class="form-control p_input">
</div>
<div class="text-center">
    <button type="submit" class="btn btn-primary btn-block enter-btn">Register</button>
</div>
</form>

共有3个答案

艾星河
2023-03-14

试试这个

$request->validate(['name' => 'required|string|min:2|max:255',
        'surname' => 'required|string|min:2|max:255',
        'email' => 'required|string|email|max:255|unique:users',
        'username' => 'required|string|max:255|unique:users',
        'password' => 'required|string|min:8|confirmed',
        'password_confirmation' => 'required']);

希望这能奏效!

樊烨烨
2023-03-14

使用$请求-

邹博裕
2023-03-14

看看注册表控制器。存根插入/vendor/laravel/ui/stubs/Auth。验证器接受一个数组,而不是一个请求对象,这是RegisterUsers传入的。

注册控制器。树桩

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

和注册用户

public function register(Request $request)
{
    $this->validator($request->all())->validate();

    event(new Registered($user = $this->create($request->all())));

    $this->guard()->login($user);

    if ($response = $this->registered($request, $user)) {
        return $response;
    }

    return $request->wantsJson()
                ? new JsonResponse([], 201)
                : redirect($this->redirectPath());
}

因此,您希望将您的函数与寄存器C中的函数相匹配ontroller.stub.

protected function validator(array $request)
{
    return Validator::make($request, ['name' => 'required|string|min:2|max:255',
        'surname' => 'required|string|min:2|max:255',
        'email' => 'required|string|email|max:255|unique:users',
        'username' => 'required|string|max:255|unique:users',
        'password' => 'required|string|min:8|confirmed',
        'password_confirmation' => 'required']);
}
 类似资料: