如何在Laravel 5中的Excel :: load块之后获取更新值?

时间:2016-06-04 07:50:24

标签: laravel-5 maatwebsite-excel

这是我的代码 - 我正在使用$rowUpdate变量并在Excel::load块中处理此变量但未获取此更新值,如何解决?

if(isset($params['workoutUploadFile'])){
                     //$path = Input::file('workoutUploadFile')->getRealPath();
                     $rowUpdate = 0;
                     $data = Excel::load($params['workoutUploadFile'], function($reader) use($request,$rowUpdate) {
                         $results = $reader->get()->toArray();
                         $row = 1;
                         foreach($results as $rowObj){//echo "<pre>";print_r($rowObj);
                             if($row == '1'){
                                 $row++;
                                 continue;
                             }
                             $checkRow = $rowObj['status'];
                             switch($checkRow){
                                 case '#WORKOUT#' :{
                                        $workoutSubCategory = \App\WorkoutCategory::select('id')
                                                                                ->where('category_name' , $rowObj['subcategory'])
                                                                                ->where('is_active' , \App\WorkoutCategory::ACTIVE)
                                                                                ->first();
                                        $workoutSubCategoryId ='';
                                        if(count($workoutSubCategory)){
                                            $workoutSubCategoryId = $workoutSubCategory['id'];
                                        }
                                        $workoutDetailData = array(
                                            'workout_name' => $rowObj['workout_name'],
                                            'workout_warmup_time' => $rowObj['warm_up_time'],
                                            'workout_cooldown_time' => $rowObj['cool_down_time'],
                                            'pre_workout_tip' => str_replace('\n','',$rowObj['pre_workout_tips']),
                                            'how_to_perform' => str_replace('\n','',$rowObj['how_to_perform']),
                                            'is_active' => \App\Workout::ACTIVE,
                                            'created_by' => $request->getSession()->get('user')['0']['id']
                                        );
                                        if(isset($rowObj['workout_id'])){
                                            $wkdetailId = $rowObj['workout_id'];
                                            $wkdetailIdForUpdate = \App\Workout::where('id',$wkdetailId)->update($workoutDetailData); 
                                            if($wkdetailIdForUpdate){
                                                $rowUpdate++;
                                            }
                                        }else{
                                            $wkdetail = \App\Workout::create($workoutDetailData);
                                        }
                                        if($rowObj['tags']!=''){
                                             $tagsArray = explode(",",$rowObj['tags']);
                                             if(count($tagsArray)){
                                                 if(isset($rowObj['workout_id'])){
                                                    $wkdetaildIdForTag = $wkdetailId;
                                                 }else{
                                                    $wkdetaildIdForTag = $wkdetail->id;
                                                 }
                                                 foreach($tagsArray as $tagVal){
                                                     $tagVal = trim($tagVal);
                                                     $checkTagExistFlag = \App\Tag::select('id')->where(array('tag_name' => $tagVal,'is_active' => \App\Tag::ACTIVE))->get()->toArray();
                                                     if(!count($checkTagExistFlag)){
                                                         $tagDetail = array(
                                                             'tag_type' => \App\Tag::TAG_TYPE_MOTIVE_BASED,
                                                             'tag_name' => $tagVal,
                                                             'is_active' => \App\Tag::ACTIVE,
                                                             'created_by' => $request->getSession()->get('user')['0']['id']
                                                         );
                                                         $tagObj = \App\Tag::create($tagDetail);
                                                         $workoutTagDetail = array(
                                                             'fk_workout_details_id' => $wkdetaildIdForTag,
                                                             'fk_tag_id' => $tagObj->id,
                                                             'is_active' => \App\Workout::ACTIVE,
                                                             'created_by' => $request->getSession()->get('user')['0']['id']
                                                         );
                                                         $wkTag = \App\WorkoutTag::create($workoutTagDetail);
                                                     }else{
                                                         $workoutTagDetail = array(
                                                             'fk_workout_details_id' => $wkdetaildIdForTag,
                                                             'fk_tag_id' => $checkTagExistFlag['0']['id'],
                                                             'is_active' => \App\Workout::ACTIVE,
                                                             'created_by' => $request->getSession()->get('user')['0']['id']
                                                         );
                                                         $checkAlreadyExistWorkoutTag = \App\WorkoutTag::where('fk_workout_details_id',$wkdetaildIdForTag)
                                                                                        ->where('fk_tag_id',$checkTagExistFlag['0']['id'])
                                                                                        ->where('is_active',\App\Workout::ACTIVE)
                                                                                        ->count();
                                                         if(!$checkAlreadyExistWorkoutTag){
                                                             $wkTag = \App\WorkoutTag::create($workoutTagDetail);
                                                         }
                                                     }
                                                 }
                                             }
                                         }
                                 } break;
                                 case '#INTERVAL#' :{
                                         $exerciseTotalDuration = '';   
                                         if(strpos($rowObj['category'],':')!==false){
                                             if(count($rowObj['category'])){
                                                $expTotDurVal = explode(":",$rowObj['category']);
                                                if(strlen($expTotDurVal[0]) == '1'){
                                                    $exerciseTotalDuration = '0'.$rowObj['category'];
                                                }else{
                                                    $exerciseTotalDuration = $rowObj['category'];
                                                }
                                             }
                                         }else{
                                             $exerciseTotalDuration = $rowObj['category'];
                                         }
                                         $workoutInterval = array(
                                             'fk_workout_details_id' => $wkdetailId,
                                             'interval_number' => $rowObj['workout_name'],
                                             'speed_zone' => $rowObj['subcategory'],
                                             'excercise_total_duration' => $exerciseTotalDuration,
                                             'is_active' => \App\WorkoutTraining::ACTIVE,
                                             'created_by' => $request->getSession()->get('user')['0']['id']
                                         );
                                         if(isset($rowObj['workout_id'])){
                                             $wkIntervalUpdateQry = \App\WorkoutTag::where('id',$rowObj['workout_caption'])
                                                                ->where('fk_workout_details_id',$wkdetailId)
                                                                ->update($workoutInterval);
                                             if($wkIntervalUpdateQry){
                                                $rowUpdate++;
                                             }
                                         }else{
                                             $wkInterval = \App\WorkoutTraining::create($workoutInterval);
                                         }
                                 } break;
                                 default:
                                         continue;
                             }
                             $row++;
                         }
                     });echo $rowUpdate;die;
                     $msg['success'] = TRUE;
                     if(isset($rowObj['workout_id'])){
                        $msg['msg'] = $rowUpdate.' row of Workout detail updated successfully!';
                     }else{
                        $msg['msg'] = 'Workout detail uploaded successfully!';
                     }
                     return json_encode($msg);
             }

变量是$rowUpdate

0 个答案:

没有答案