如何从一个函数启动事务并在laravel中的另一个函数中结束它

时间:2017-10-28 07:56:12

标签: php mysql laravel api

我有两个编辑配置文件API的功能:

1)编辑个人资料 2)验证OTP

我需要使用验证OTP功能验证移动设备(如果已更改)。为此,我需要COMMIT我的更改(更新移动)只有当用户提供OTP我想要ROLLBACK。所以,基本上我需要从editProfile()开始事务并以verifyOtp()结束。如何实施?请帮忙。

public function editProfile(Request $request)
    {
        $user = Auth::user();
        $Helper = new Helper;

        $validator = Validator::make($request->all(), 
        ['first_name' => 'required|alpha|string|max:255',
        'last_name' => 'required|alpha|string|max:255',
        'mobile' => 'required|digits:10|unique:users,mobile,'.$user->id,
        'user_image' => 'image|mimes:jpeg,png,jpg,gif,svg|max:2000'
         ]);


         if($validator->fails()) 
         {
           $error_array = (array)$validator->errors();
           $error_string =  $Helper->ModifyError($error_array);
           return response()->json(['ERROR'=>['MESSAGES'=>$error_string],'API_STATUS'=>'FAIL'], 200);            
         }

        $input = $request->all();
        if(isset($input['user_image']))
        {
            $user_upload_folder_path   = public_path('uploads/users/'.$user->id);
            if(!is_dir($user_upload_folder_path))
                {
                    mkdir($user_upload_folder_path);
                    chmod($user_upload_folder_path,0777);
                }

            $imageName = time().substr(pathinfo($request->user_image->getClientOriginalName(),PATHINFO_FILENAME),0,10).'.'.$request->user_image->getClientOriginalExtension();
            $request->user_image->move($user_upload_folder_path, $imageName);
            $input['user_image']=$imageName;
        }
        else
        {
            $input['user_image']='';
        }


        if($input['mobile']!=$user->mobile)
        {
            $input['otp']=(int)mt_rand(10000,99999);
            $message='Thanks for registering to zvesta. The verification OTP is '.$input['otp'];
            $this->send_sms($input['mobile'],$message); 
            $data=array(
            'first_name'=>$input['first_name'],
            'last_name'=>$input['last_name'],
            'mobile'=>$input['mobile'],
            'user_image'=>$input['user_image']);
            $data=json_encode($data);
            User::whereId($user->id)->update(['otp'=>$input['otp']]);
            DB::table('temporary')->
            insert([
                'user_id'=>Auth::id(),
                'data'=>$data
            ]);
            $success['mobile_verified']=0;

        }
        else
        {
            $user = User::whereId($user->id)->update([
            'first_name'=>$input['first_name'],
            'last_name'=>$input['last_name'],
            'user_image'=>$input['user_image']
            ]);
            $success['mobile_verified']=1;
        }
        return response()->json(['SUCCESS'=>$success,'API_STATUS' => 'SUCCESS'], $this->successStatus); 
    }

    public function verifyOtp(Request $request)
    {
        $input=$request->all();
        $user=User::where('otp',$input['otp'])->where('id',Auth::id())->first();
        if(!empty($user))
        {
            $temp_data=DB::table('temporary')->where('user_id',Auth::id())->select('data')->first();
            $temp_data=json_decode($temp_data->data,true);
            User::where('id',Auth::id())->update([
                'first_name'=>$temp_data['first_name'],
                'last_name'=>$temp_data['last_name'],
                'mobile'=>$temp_data['mobile'],
                'user_image'=>$temp_data['user_image'],
                'otp'=> null
            ]);
            DB::table('temporary')->where('user_id',Auth::id())->delete();
            return response()->json(['API_STATUS'=>'SUCCESS'], $this->successStatus); 
        }
        else
        {
            return response()->json(['API_STATUS'=>'FAIL'], $this->successStatus); 
        }
    }

1 个答案:

答案 0 :(得分:1)

您可以使用DB::beginTransaction()DB::rollBack()DB::commit()方法代替DB::transaction(callback)

https://laravel.com/docs/5.5/database#database-transactions