title: Issue in Re-Register In my application
body: <p>Hey I Have an application.In which when user register before entering into application.The problem is i am using otp verification.But while making otp request sometimes otp doesn't comes and user fails to register into application and if user want to re-register he is not able to re-register as in database his details are saved and according to this code he is not able to re-request for otp.Here i am embedding my php code.Kindly help me in if otp verified then only data saved into my database.</p>
  
  <pre><code>&lt;?php
  namespace App\Http\Controllers\Api;
  
  use Illuminate\Http\Request;
  
  use App\Http\Controllers\Controller;
  use Illuminate\Support\Facades\Validator;
  use Auth;
  use App\User;
  use App\Otp;
  use App\Referal;
  use App\Constant;
  
  class registerController extends Controller
  {
      public function apiRegister(Request $request)
      {
          $this-&gt;Validate($request,[
              'name' =&gt; 'required|string',
              'mobile' =&gt; 'required|unique:users,mobile|min:10|max:10',
              'email' =&gt; 'required|email|unique:users,email',
              'password' =&gt; 'required|min:6',
              'refer_code' =&gt; 'nullable|exists:users,refer_code',
              'device_id' =&gt; 'required'
          ]);
  
  
  
      $device = User::where('device_id',$request-&gt;device_id)-&gt;first();
  
      if (!empty($device) &amp;&amp; $device-&gt;otp_verified == 'n') {
  
          $apiToken = str_random(150);
          $otp = mt_rand(1000,9999);
          $msg = 'Your OTP is: '.$otp;
          $ot = sendSms($request-&gt;mobile,$msg);
          if($ot == true)
          {
              User::create([
                  'name' =&gt; $request-&gt;name,
                  'mobile' =&gt; $request-&gt;mobile,
                  'email' =&gt; $request-&gt;email,
                  'password' =&gt; bcrypt($request-&gt;password),
                  'api_token' =&gt; $apiToken,
                  'user_type' =&gt; 'u',
                  'points' =&gt; 0,
                  'refer_code' =&gt; refferCode(),
                  'otp' =&gt; $otp,
                  'ip_address' =&gt; \Request::getClientIp(true),
                  'device_id' =&gt; $request-&gt;device_id
              ]);
  
              $user_id = User::where('mobile',$request-&gt;mobile)-&gt;select('id','refer_code')-&gt;first();
  
              if(isset($request-&gt;refer_code)){    // if referal code is entered by user
  
                  $refer_user = User::where('refer_code',$request-&gt;refer_code)-&gt;first();
  
                  if(strtoupper($refer_user-&gt;refer_code) == strtoupper($request-&gt;refer_code))
                  {
                      $const = Constant::where('constant_name','referal_points')-&gt;first();
  
                      Referal::create([
                          'refer_from_id' =&gt; $refer_user-&gt;id,
                          'refer_to_id' =&gt; $user_id-&gt;id,
                          'referal_points' =&gt; $const-&gt;constant_value,
                          'time' =&gt; time()
                      ]);
                  }   
                  else
                  {
                      $response['msg'] = 'Referal Code is invalid!! try with another Referal code!!';
                      return response()-&gt;json($response,400);
                  }
              }
  
              Otp::create([
                  'user_id' =&gt; $user_id-&gt;id,
                  'time' =&gt; time(),
              ]);
  
              $response['msg'] = 'User Registered Successfully!';
              $response['refer_code'] = 'Your Referal Code :'. $user_id-&gt;refer_code;
              return response()-&gt;json($response,200);
          }
          else
          {
              $response['msg'] = 'User Registreation Unsuccessful!  Try again!!!';
              return response()-&gt;json($response,400);
          }
      }elseif(empty($device))
      {
          $apiToken = str_random(150);
          $otp = mt_rand(1000,9999);
          $msg = 'Your OTP is: '.$otp;
          $ot = sendSms($request-&gt;mobile,$msg);
          if($ot == true)
          {
              User::create([
                  'name' =&gt; $request-&gt;name,
                  'mobile' =&gt; $request-&gt;mobile,
                  'email' =&gt; $request-&gt;email,
                  'password' =&gt; bcrypt($request-&gt;password),
                  'api_token' =&gt; $apiToken,
                  'user_type' =&gt; 'u',
                  'points' =&gt; 0,
                  'refer_code' =&gt; refferCode(),
                  'otp' =&gt; $otp,
                  'ip_address' =&gt; \Request::getClientIp(true),
                  'device_id' =&gt; $request-&gt;device_id
              ]);
  
              $user_id = User::where('mobile',$request-&gt;mobile)-&gt;select('id','refer_code')-&gt;first();
  
              if(isset($request-&gt;refer_code)){    // if referal code is entered by user
  
                  $refer_user = User::where('refer_code',$request-&gt;refer_code)-&gt;first();
                  if($refer_user-&gt;refer_code == $request-&gt;refer_code)
                  {
                      $const = Constant::where('constant_name','referal_points')-&gt;first();
                      Referal::create([
                          'refer_from_id' =&gt; $refer_user-&gt;id,
                          'refer_to_id' =&gt; $user_id-&gt;id,
                          'referal_points' =&gt; $const-&gt;constant_value,
                          'time' =&gt; time(),
                      ]);
                  }   
                  else
                  {
                      $response['msg'] = 'Referal Code is invalid!! try with another Referal code!!';
                      return response()-&gt;json($response,400);
                  }
              }
  
              Otp::create([
                  'user_id' =&gt; $user_id-&gt;id,
                  'time' =&gt; time(),
              ]);
  
              $response['msg'] = 'User Registered Successfully!';
              $response['refer_code'] = 'Your Referal Code :'. $user_id-&gt;refer_code;
              return response()-&gt;json($response,200);
          }
          else
          {
              $response['msg'] = 'User Registreation Unsuccessful!  Try again!!!';
              return response()-&gt;json($response,400);
          }
  
      }
      else
      {
          $response['msg'] = 'This device has been used previously. Please try with another device!';
          return response()-&gt;json($response,400);
      }
  
  }
  </code></pre>
  
  <p>}</p>