Laravel 4: avoid duplicate entries

My application has a simple form with one field (email), which makes it possible to register in the newsletter.

If I enter a new letter, everything works fine. If I enter a letter that already exists in the database, I get the error SQLSTATE [23000]: violation of integrity constraint: 1062 Duplicate record ... Because I defined this field as unique in the database.

All I want to do is redirect :: back () → from ("message", "letter has already been registered") But I do not know how I can do this? Can I just put an if statement in a method controller? Or I have to define it in $ rules in the model by adding another rule:

public static $rules = array( 'email' => 'required',); 

Thanks!

+4
source share
1 answer

Just define a unique rule in your users table:

 public static $rules = array( 'email' => 'required|unique:users|email'); 
+4
source

Source: https://habr.com/ru/post/1500140/


All Articles