title: How to handle duplicate entry for email error?
body: <p>I have a form where admin can add users. When I submit form with some email that exist in database I get error saying</p>
  
  <blockquote>
    <p>SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'mail@mail.com' for key 'users_email_unique'</p>
  </blockquote>
  
  <p>I want to avoid that error and instead to get for example warning "mail is taken" or something similar. Any help is appreciated. Here is my code.</p>
  
  <p>UserController.php</p>
  
  <pre><code>public function store(StoreUserInfo $data)
  {
      $data-&gt;validated();
  
      $user = User::create([
          'first_name' =&gt; $data['first_name'],
          'last_name' =&gt; $data['last_name'],
          'email' =&gt; $data['email'],
          'password' =&gt; Hash::make($data['password']),
          'city' =&gt; $data['city']
      ]);
  
       return redirect()
          -&gt;route('admin.users')
          -&gt;with('message', 'User created successfully');
  }
  </code></pre>
  
  <p>StoreUserInfo.php</p>
  
  <pre><code>public function rules()
  {
      $emailid = (Auth::user()-&gt;roles()-&gt;first()-&gt;name == 'admin')
          ? (isset($this-&gt;user-&gt;id)?$this-&gt;user-&gt;id:Auth::user()-&gt;id) : Auth::user()-&gt;id;
  
      return [
  
          'first_name' =&gt; ['required', 'string', 'max:255'],
          'last_name' =&gt; ['required', 'string', 'max:255'],
          'email' =&gt; ['required', 'string', 'email', 'max:255', 'unique:users,email,' . $emailid],
          'city' =&gt; ['required', 'exists:cities,city']
      ];
  }
  </code></pre>
  
  <p>register.blade.php</p>
  
  <pre><code>&lt;form id="form"method="POST" action="{{ route('admin.user.store') }}"&gt;
  
      @csrf
  
      &lt;div class="form-group row"&gt;
          &lt;label for="first_name" class="col-md-4 col-form-label text-md-right"&gt;{{ __('First Name') }}&lt;/label&gt;
  
          &lt;div class="col-md-6"&gt;
              &lt;input id="name" type="text" class="form-control @error('first_name') is-invalid @enderror" name="first_name" value="{{ old('first_name') }}" required autocomplete="first_name" autofocus&gt;
  
              @error('first_name')
                  &lt;span class="invalid-feedback" role="alert"&gt;
                      &lt;strong&gt;{{ $message }}&lt;/strong&gt;
                  &lt;/span&gt;
              @enderror
          &lt;/div&gt;
      &lt;/div&gt;
  
      &lt;div class="form-group row"&gt;
              &lt;label for="last_name" class="col-md-4 col-form-label text-md-right"&gt;{{ __('Last Name') }}&lt;/label&gt;
  
              &lt;div class="col-md-6"&gt;
                  &lt;input id="name" type="text" class="form-control @error('last_name') is-invalid @enderror" name="last_name" value="{{ old('last_name') }}" required autocomplete="last_name" autofocus&gt;
  
                  @error('last_name')
                      &lt;span class="invalid-feedback" role="alert"&gt;
                          &lt;strong&gt;{{ $message }}&lt;/strong&gt;
                      &lt;/span&gt;
                  @enderror
              &lt;/div&gt;
          &lt;/div&gt;
  
      &lt;div class="form-group row"&gt;
          &lt;label for="email" class="col-md-4 col-form-label text-md-right"&gt;{{ __('E-Mail Address') }}&lt;/label&gt;
  
          &lt;div class="col-md-6"&gt;
              &lt;input id="email" type="email" class="form-control @error('email') is-invalid @enderror" name="email" value="{{ old('email') }}" required autocomplete="email"&gt;
  
              @error('email')
                  &lt;span class="invalid-feedback" role="alert"&gt;
                      &lt;strong&gt;{{ $message }}&lt;/strong&gt;
                  &lt;/span&gt;
              @enderror
          &lt;/div&gt;
      &lt;/div&gt;
  
      &lt;div class="form-group row"&gt;
          &lt;label for="password" class="col-md-4 col-form-label text-md-right"&gt;{{ __('Password') }}&lt;/label&gt;
  
          &lt;div class="col-md-6"&gt;
              &lt;input id="password" type="password" class="form-control @error('password') is-invalid @enderror" name="password" required autocomplete="new-password"&gt;
  
              @error('password')
                  &lt;span class="invalid-feedback" role="alert"&gt;
                      &lt;strong&gt;{{ $message }}&lt;/strong&gt;
                  &lt;/span&gt;
              @enderror
          &lt;/div&gt;
      &lt;/div&gt;
  
      &lt;div class="form-group row"&gt;
          &lt;label for="password-confirm" class="col-md-4 col-form-label text-md-right"&gt;{{ __('Confirm Password') }}&lt;/label&gt;
  
          &lt;div class="col-md-6"&gt;
              &lt;input id="password-confirm" type="password" class="form-control" name="password_confirmation" required autocomplete="new-password"&gt;
          &lt;/div&gt;
      &lt;/div&gt;
      &lt;div class="form-group row"&gt;
              &lt;label for="city" class="col-md-4 col-form-label text-md-right"&gt;{{ __('Select City') }}&lt;/label&gt;
  
              &lt;input name="city" list="result" id="input" value="{{ old('city') }}" class="form-control  @error('city') is-invalid @enderror col-sm-6 custom-select custom-select-sm"required autocomplete="city"&gt;
              &lt;datalist id="result"&gt;
              &lt;/datalist&gt;
  
              @error('city')
                  &lt;span class="invalid-feedback" role="alert"&gt;
                      &lt;strong&gt;{{ $message }}&lt;/strong&gt;
                  &lt;/span&gt;
              @enderror
      &lt;/div&gt;
  
  &lt;/form&gt;
  </code></pre>
  
  <p>users table</p>
  
  <pre><code>public function up()
  {
      Schema::create('users', function (Blueprint $table) {
          $table-&gt;increments('id');
          $table-&gt;string('first_name');
          $table-&gt;string('last_name');
          $table-&gt;string('email')-&gt;unique();
          $table-&gt;timestamp('email_verified_at')-&gt;nullable();
          $table-&gt;string('password');
          $table-&gt;string('city');
          $table-&gt;rememberToken();
          $table-&gt;timestamps();
      });
  }
  </code></pre>