Laravel 过程声明(字符串$attribute,$value):bool必须与illumb\Contracts\Validation\Rule::passes($attribute,$value)兼容

Laravel 过程声明(字符串$attribute,$value):bool必须与illumb\Contracts\Validation\Rule::passes($attribute,$value)兼容,laravel,laravel-5,laravel-5.5,Laravel,Laravel 5,Laravel 5.5,我已经创建了一个自定义验证规则 namespace App\Rules; use Illuminate\Contracts\Validation\Rule; use Exception; class ValidFoo implements Rule { /** * Determine if the validation rule passes. * * @param string $attribute * @param mixed $v

我已经创建了一个自定义验证规则

namespace App\Rules;

use Illuminate\Contracts\Validation\Rule;
use Exception;

class ValidFoo implements Rule
{
    /**
     * Determine if the validation rule passes.
     *
     * @param  string  $attribute
     * @param  mixed  $value
     * @return bool
     */
    public function passes(string $attribute, $value): bool
    {
        if (!$foo) {
            return false;
        }

        return true;
    }

    /**
     * Get the validation error message.
     *
     * @return string
     */
    public function message(): string
    {
        return 'The foo you \'ve provided is not valid.';
    }
}
然而,当我试图提交表单时,我得到了这个错误

Symfony\Component\Debug\Exception\FatalErrorException (E_UNKNOWN)
Declaration of App\Rules\ValidFoo::passes(string $attribute, $value): bool must be compatible with Illuminate\Contracts\Validation\Rule::passes($attribute, $value)
这是拉威尔的规则界面

namespace Illuminate\Contracts\Validation;

interface Rule
{
    /**
     * Determine if the validation rule passes.
     *
     * @param  string  $attribute
     * @param  mixed  $value
     * @return bool
     */
    public function passes($attribute, $value);

    /**
     * Get the validation error message.
     *
     * @return string
     */
    public function message();
}
这是一门批改过的课。发生错误,因为您使用了类型hit,但接口未使用类型提示

namespace App\Rules;

use Illuminate\Contracts\Validation\Rule;
use Exception;

class ValidFoo implements Rule
{
    /**
     * Determine if the validation rule passes.
     *
     * @param  string  $attribute
     * @param  mixed  $value
     * @return bool
     */
    public function passes($attribute, $value)
    {
        if (!$foo) {
            return false;
        }

        return true;
    }

    /**
     * Get the validation error message.
     *
     * @return string
     */
    public function message(): string
    {
        return 'The foo you \'ve provided is not valid.';
    }
}