传递给验证器()的 Laravel 参数 1 必须是 Illuminate\Http\Request 的实例,给定数组

Posted

技术标签:

【中文标题】传递给验证器()的 Laravel 参数 1 必须是 Illuminate\\Http\\Request 的实例,给定数组【英文标题】:Laravel Argument 1 passed to validator() must be an instance of Illuminate\Http\Request, array given传递给验证器()的 Laravel 参数 1 必须是 Illuminate\Http\Request 的实例,给定数组 【发布时间】:2021-08-20 16:27:03 【问题描述】:

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

在我正在使用的 web.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');


这里是 register.blade.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>

【问题讨论】:

【参考方案1】:

查看/vendor/laravel/ui/stubs/Auth 中的RegisterController.stub。验证器接受一个数组,而不是一个 Request 对象,这是 RegistersUsers 传入的。

RegisterController.stub

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());

因此,您希望将您的函数与 RegisterController.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']);

【讨论】:

【参考方案2】:

使用$request-&gt;validate([...]) 而不是$this-&gt;validate($request, [...])

【讨论】:

【参考方案3】:

试试这个

$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']);

希望这行得通!

【讨论】:

【参考方案4】:

试试这个(RegisterController)

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


public function store(Request $request)
    
    $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',
]);

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


我希望这有效!

【讨论】:

以上是关于传递给验证器()的 Laravel 参数 1 必须是 Illuminate\Http\Request 的实例,给定数组的主要内容,如果未能解决你的问题,请参考以下文章

传递给 Illuminate\Database\Grammar::parameterize() 的 Laravel 工厂参数 1 必须是数组类型,给定字符串

传递给 GuzzleHttp\Client::send() 的参数 1 必须实现接口

Jwt身份验证错误参数3传递给Lcobucci JWT Signer Hmac :: doVerify()

Laravel 表单请求未传递给参数

我收到此错误“传递给Illuminate Database Grammar :: parameterize()的参数1必须是类型

Laravel 4:将从存储库获得的验证消息传递给控制器