Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Validation rule unique requires at least 1 parameters

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

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'];
like image 840
Candra Herk Avatar asked Jan 31 '18 03:01

Candra Herk


3 Answers

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'
    ]);
like image 179
Sagar Gautam Avatar answered Nov 16 '22 03:11

Sagar Gautam


Why dont you just use like this

L5.5

public function postUbah(Request $request, $id)

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

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

class User {
    protected $table = "user";
}
like image 40
ZeroOne Avatar answered Nov 16 '22 02:11

ZeroOne


Use "unique:users" instead of "unique:user". This can work for some cases.

like image 44
Mostafa Rafid Avatar answered Nov 16 '22 01:11

Mostafa Rafid