laravel 验证规则唯一需要至少 1 个参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/48533921/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 17:19:06  来源:igfitidea点击:

Validation rule unique requires at least 1 parameters

phplaravelilluminate-container

提问by Candra Herk

I've got problem with my laravel I can't post the data

我的 Laravel 有问题,我无法发布数据

One error says

一个错误说

InvalidArgumentException in Validator.php line 2593:
Validation rule unique requires at least 1 parameters.

And here it's my code

这是我的代码

public function postUbah(Request $request, $id)

    $validator  = Validator::make($request->all(), [
            'username'  => 'required|unique:user|min:5',
            'name'      => 'required',
            'group'     => 'required'
        ]);
}

Thanks for the help.

谢谢您的帮助。

Here it's my model

这是我的模型

class User extends Model implements AuthenticatableContract, CanResetPasswordContract {

    use Authenticatable, CanResetPassword;


    protected $table = 'user';


    protected $fillable = ['username', 'password'];


    protected $hidden = ['password', 'remember_token'];

回答by Sagar Gautam

You've forgot to add the parameter in validation rule to specify the database table column which should be unique in validation rule. You can do it like,

您忘记在验证规则中添加参数来指定在验证规则中应该是唯一的数据库表列。你可以这样做,

 $validator  = Validator::make($request->all(), [
        'username'  => 'required|unique:user,name|min:5',
        'name'      => 'required',
        'group'     => 'required'
    ]);

回答by ZeroOne

Why dont you just use like this

你为什么不这样使用

L5.5

L5.5

public function postUbah(Request $request, $id)

    $validator  = $request->validate([
            'username'  => 'required|unique:user|min:5',
            'name'      => 'required',
            'group'     => 'required'
        ]);
}

L5.4

L5.4

public function postUbah(Request $request, $id)
    $validator  = $this->validate($request, [
            'username'  => 'required|unique:user|min:5',
            'name'      => 'required',
            'group'     => 'required'
        ]);
}

Im aware that your table name is user.. make sure your User Model have

我知道你的表名是user.. 确保你的用户模型有

class User {
    protected $table = "user";
}