简体   繁体   中英

Validation form unique Laravel 5.4 doesn't work

I have this simple code:

$this->validate($request, [
    'email' => 'required|email|unique:subscriptions'
]);

However I can keep putting in the same email and it doesn't send me back and error and keeps creating the row in the database.

However if I change the code to ..

$this->validate($request, [
    'email' => 'required|email|unique:users'
]);

It sends me back an error properly and displays in the div I have setup for it.

I have tried many different ways to fix this, but I feel like it should be easier than this. I looked at everything and it doesn't seem to address this issue. Any help would be great.

You need to specify the column name

 $this->validate($request, [
    'email' => 'required|email|unique:subscriptions,email'
   ]);  

  $this->validate($request, [
        'email' => 'required|email|unique:users,email'
  ]);

While updating you need to force a unique rule to Ignore Given ID

'email' => 'unique:subscriptions,email,'.$user->id

https://laravel.com/docs/5.2/validation#rule-unique

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM