noobmaster
noobmaster

Reputation: 45

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.

Upvotes: 0

Views: 220

Answers (1)

sumit
sumit

Reputation: 15464

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

Upvotes: 1

Related Questions