Laravel Eloquent rollback does not work, for queries separated by PHP functions

ぐ巨炮叔叔 提交于 2019-12-11 06:35:47

问题


I separate the model queries on a trait class, because I hate to read a long block of codes with the model queries, and because I find it convenient if I reused the same function. But I found a problem once an error occur.

I was trying to run rollback function from eloquent once an error occur but, unfortunately, rollback wont work as I am expecting.

Am I doing it wrong? Are there any other ways to implement this?

    try {
        DB::beginTransaction();


        // UserDetails
        $userdetailsID = $this->saveUserDetails($request,$userData->id);

        if($userdetailsID){
            $result = $this->saveUser(
                $request,
                $this->getHashValue($request->password),
                $userdetailsID,
                $userData->id,
                $this->cleanTobeSafeAsDirectory(crypt(($userdetailsID.$userData->companyid), 'rl'))
                );

            if($result){
                $updalodResult = $this->uploadThisImage( $request , 'images/uploads/users/'.$userdetailsID.'/icon/', '_'.$this->getHashValue($userdetailsID),'userImageAvatar');
                if($updalodResult['success']){
                    $resul = $this->getThisUserDetials($userdetailsID);
                    $resul->photo_name = $updalodResult['filename'];
                    $resul->save();

                    $imageFilePath = 'images/uploads/users/'.$userdetailsID.'/icon/'.$updalodResult['filename'];
                    $this->cropImageJpegOnly($imageFilePath,$request->img_x,$request->img_y,$request->img_w,$request->img_h,$request->img_width,$request->img_height);
                }
                DB::commit();
                return $this->returnAsAppSuccess('User information added.');
            }
            DB::rollBack();
            return $this->returnAsAppError('Failed to user security details.' );
        }


        DB::rollBack();
        return $this->returnAsAppError('Failed to user security details.' );
    } catch (PDOException $e) {
        DB::rollBack();
        return $this->returnAsAppError('Failed to insert User information.');
    }

来源:https://stackoverflow.com/questions/48422221/laravel-eloquent-rollback-does-not-work-for-queries-separated-by-php-functions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!