2016-03-27 3 views
1

を使用するよりもタプルを更新できません。私がこの機能を使って何をしているのかは、入力された携帯番号を持つユーザーが存在する場合、デバイスの詳細(deviceIdとdeviceType)を更新する必要があります。そうでない場合は、新しいユーザーを作成し、データベースに挿入されます。いずれの場合も現在のタプルを返す必要があります。いずれかが返された場合はエラーも返されます。はかつて私が入力JSON(入力JSONのフィールド移動可能である、USERTYPEとデバイスとDEVICETYPE)で、loginメソッドを呼び出していますLaravelクエリビルダ

私が直面してる問題は、私は(新しいデバイスIDとDEVICETYPE付き)既存のユーザーの携帯電話番号を挿入すると(必要に応じて)、それはそのユーザを更新していることです。しかし、もう一度やり直すと、「デバイスの詳細を更新中にエラーが発生しました。」というメッセージが返されます。特定のクエリが初めて正しく実行される可能性はありますが、それ以降は正しくは理解できません。私を助けてください。前もって感謝します。

public function login($data){ 
    $returnData = new \stdClass(); 
    if(empty($data['mobile']) || empty($data['userType'])) { 
     $returnData->response = "error"; 
     $returnData->message = "Insufficient Input"; 

     return json_encode($returnData); 
    } 
    $recd = DB::table('users') 
    ->where('mobile',$data['mobile']) 
    ->where('userType',$data['userType']) 
    ->first(); 

    if(!empty($recd)){//user exists, just return the values 
     if(isset($data['mobile'])){ 
      $updateDeviceDetails = DB::table('users') 
           ->where('mobile',$data['mobile']) 
           ->update(['deviceId'=>$data['deviceId'], 'deviceType'=>$data['deviceType']]); 
     } else { 
      echo "mobile not set."; 
     } 
     if($updateDeviceDetails){ 
      $updatedUser = DB::table('users') 
          ->where('id',$recd->id) 
          ->get(); 
      $returnData->response = "success"; 
      $recd->isNewUser = "0"; 
      $returnData->data = $updatedUser; 

      return json_encode($returnData); 
     } else { 
      $returnData->response = "error"; 
      $returnData->message = "Error updating the device details."; 

      return json_encode($returnData); 
     } 
    } else {//user does not exist, create new user 
     if(empty($data['deviceId']) || empty($data['accessToken']) || empty($data['deviceType'])){ 
      $returnData->response = "error"; 
      $returnData->message = "Insufficient Input"; 
      return json_encode($returnData); 
     } 
     $data['created_at'] = $data['updated_at'] = Carbon::now('Asia/Kolkata'); 
     $data['otp'] = mt_rand(100000, 999999); 
     $data['otpStatus'] = 0; 
     $data['userType'] = 1; 
     //insert new user 
     $newUserId = DB::table('users')->insertGetId($data); 
     if ($newUserId>0) { 

      //get the newly inserted user 
      $newUser = DB::table("users") 
      ->where('id',$newUserId) 
      ->first(); 

      //newUser is the object to be returned 
      $newUser->isNewUser = "1"; 
      $returnData->response = "success"; 
      $returnData->data = $newUser; 
     } else { 
      $returnData->response = "error"; 
      $returnData->message = "Error creating new user."; 
     } 
     return json_encode($returnData); 
    } 
} 

答えて

0

$updateDeviceDetailsには、何かがタプルで更新されたときにのみtrueまたは1が含まれます。更新するすべてのフィールドに対して同じ値を再度挿入すると、falseが返されます。

このタプルに既に存在する値をそれぞれの列に対してupdate関数に渡していたので、$updateDeviceDetailsではtrueになりませんでした。

関連する問題