首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法使用Laravel查询构建器多次更新元组

无法使用Laravel查询构建器多次更新元组
EN

Stack Overflow用户
提问于 2016-03-28 04:08:54
回答 1查看 72关注 0票数 1

我使用输入json调用登录方法(输入json中的字段是mobile、userType、deviceId和deviceType)。我使用此函数所做的是,如果输入手机号码的用户存在,则应更新设备详细信息(deviceId和deviceType),否则,如果不存在此类用户,则应创建一个新用户,并将其所有详细信息插入到数据库中。无论哪种情况,都应该返回当前的元组。如果有错误,也会返回。

我面临的问题是,当我插入一个已经存在的用户的手机号码(使用新的deviceId和deviceType)时,它会根据需要更新该用户。但当我再次执行此操作时,它会返回“更新设备详细信息时出错”。我不知道一个特定的查询怎么可能在第一次正确运行,但在那之后就不正确了。请帮帮我。提前谢谢。

代码语言:javascript
复制
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);
    }
}
EN

回答 1

Stack Overflow用户

发布于 2016-03-28 11:26:33

仅当元组中的内容更新时,$updateDeviceDetails才包含true或1。如果我们再次为所有要更新的字段插入相同的值,它将返回false。

我将这个元组中已经存在的值,对于它们各自的列,赋给了update函数,因此在$updateDeviceDetails中没有实现。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36252064

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档