Insert and update record laravel

Submitted by Darshan Malani - 6 months ago

Laravel in insert udpate record..

0) Angular js controller
    vm.logWeight = function(){
				vm.log_loader = true;

				var weight_data = {};

				weight_data.current_weight = vm.log_weight.current_weight;
				if(vm.validBodyFat(vm.log_weight.body_fat) == true){
					weight_data.body_fat = vm.log_weight.body_fat;
				}
				
				weight_data.user_id = vm.userId;
				weight_data.created_by = vm.userId;
				weight_data.updated_by = vm.userId;

				weight_data.log_date = moment(vm.log_weight.log_date).format('YYYY-MM-DD');
				weight_data.guid_server = true;
				weight_data.source = "MANUAL";
				StoreService.save('api/v1/weight/log_weight', weight_data).then(function(result) {
					vm.user_weights_history.push(weight_data);
					if(moment(weight_data.log_date).format('YYYY-MM-DD') > moment(before_six_day).format("YYYY-MM-DD")) {
						vm.user_weights.push(weight_data);
						vm.weightCharts(vm.weightGraph);
					} 
					

					vm.last_log_date = moment(weight_data.log_date).format('MMM-DD-YYYY');
					vm.log_weight = {};
					vm.log_weight.log_date = new Date();
					vm.log_loader = false;

					if(result.error == false) {
						showFlashMessage('Logged','Weight logged successfully','success');
						vm.loggedWeight();
					}
					
				});
			
			}
            
1) Route File
   Route::post('/weight/log_weight', 'WeightController@logWeight');
2) 

 public function logWeight(Request $request){

        $error = false;
        $status_code = 200;
        $result =[];
        $input = $request->all(); 
        $today = date("Y-m-d");
        $user_agent = "";

        if(!empty($input)) {
            try{
                
                $user_id = $input['user_id'];

                $user = \App\Models\User::patient()->find($user_id);
                
                if($user != null){
                  
                    if(empty($input['log_date'])){
                        $input['log_date'] = $today;
                    }
                    
                    if(isset($input['guid_server'])){
                        
                        $current_timestamp = strtotime(date("Y-m-d H:i"));
                        
                        $input['guid'] = guid();
                        $input['timestamp'] = $current_timestamp;
                        $user_agent = 'server';
                    }

                    $check_user_weight = $user->weight()->where('timestamp', $input['timestamp'])
                                                      ->first();

                    if(!empty($check_user_weight)){
                        
                        $check_user_weight->update($input);
                        $id = $check_user_weight->id;
                        $guid = $check_user_weight->guid;
                    }
                    else{
                        
                        $log_weight = $user->weight()->create($input);
                        $id = $log_weight->id;
                        $guid = $log_weight->guid;
                    }
                    
                    //update user weight in profile
                    $profile_values = array("baseline_weight" => $input['current_weight']);
                    $user->profile()->update($profile_values);

                    // for iPhone notification
                    if($user_agent != "" && $user_agent == 'server'){
                        $notification['user_id'] = $user_id;
                        $notification['notification_text'] = 'WEIGHT_ADDED';
                        sendNotification($notification);

                    }
                    
                    $result['log_weight'] = array("id"=>$id, "guid"=>$guid);
                    $result['message']='Weight Logged Successfully.';
                }else{

                    $error = true;
                    $status_code = 404;
                    $result['message']='No Patient Found.';
                }
                
               
            }catch(\Exception $ex){
                throw $ex;
                $error = true;
                $status_code = $ex->getCode();
                $result['error_code'] = $ex->getCode();
                $result['error_message'] = $ex->getMessage();
            }
             
        }else{
            $error =true;
            $status_code = 400;
            $result['message']='Request data is empty';
        }
       
        return response()->json(['error' => $error,'response'=>$result],$status_code)
                         ->setCallback(\Request::input('callback'));

    }
comments powered by Disqus

Ads cover the costs of maintaining Laravel-tricks