Laravel MySQL多行插入跳过现有记录

时间:2018-12-27 06:08:35

标签: laravel foreach eloquent

我有2个多重数组,我循环遍历以在数据库中插入新值,但是当我添加验证时,当条件为true时,循环终止,其余查询不会被执行

{
initialRouteName: 'Home',
drawerPosition: 'left',
contentComponent: withUserContext(CustomDrawerContentComponent),
drawerOpenRoute: 'DrawerOpen',
drawerToggleRoute: 'DrawerToggle',
drawerCloseRoute: 'DrawerClose',
style:{
  drawer: {width:200, shadowColor: '#000000', shadowOpacity: 0.8, shadowRadius: 3},
    main: {paddingLeft: 3}
},
contentOptions: {
  activeTintColor: "white",
  activeBackgroundColor: 'transparent',
  inactiveTintColor: '#c5e6fc',
  inactiveBackgroundColor: 'transparent',
  labelStyle: {
    fontSize: 15,
    marginLeft: 10,
    },
  },
}

我希望其他部分将跳过它们,然后运行其余所有查询

1 个答案:

答案 0 :(得分:1)

我解决了问题

    public function create_user_course(Request $request)
    { 
        $this->validate($request, [
            'select_user_email' => 'required',
            'select_course_assign' => 'required',
        ]);

        $user_email = $request->select_user_email;
        $course_assign = $request->select_course_assign;
                foreach ($user_email as $key => $email) {
                            foreach ($course_assign as $key => $course) {

                                $course_details = Course::find($course);
                                $topic_data = json_decode($course_details->topic_list);
                                for ($i = 0; $i < count($topic_data); $i++) {
                                    for ($j = 0; $j < count($topic_data[$i]); $j++) {
                                        $topic_name_data [] = $topic_data[$i][$j]->name;
                                        $topic_id_data [] = $topic_data[$i][$j]->id;
                                    }
                                }
                                $userCourse = new UserCourse([
                                    'course_id' => $course_details->id,
                                    'course_image' => str_replace('public/', '', $course_details->image),
                                    'qty' => '1',
                                    'price' => $course_details->total_price,
                                    'access_duration' => $course_details->access_duration,
                                    'course_duration' => $course_details->video_duration,
                                    'price' => $course_details->total_price,
                                    'topic_names' => json_encode($topic_name_data),
                                    'topic_ids' => json_encode($topic_id_data),
                                ]);

                                $user = User::find($email);

                                // validation check
                                $check_usercourse_profile_count=DB::table('user_courses')
                                    ->select('*')
                                    ->where('course_id',$course_details->id)
                                    ->where('user_id',$user->id)
                                    ->count();
                                
                                    if($check_usercourse_profile_count==0){
    
                                        $result = $user->usercourse()->save($userCourse);
                                    }
                                    else{
                                        $course = next($course_assign);
                                    }
                                }
                        if($check_usercourse_profile_count==0){

                            $result = $user->usercourse()->save($userCourse);
                        }
                        else{
                            $user = next($user_email);
                        }   
                    }
    if($check_usercourse_profile_count==0){
    return redirect()->route('admin.user.assign.course')->with('status', 'Course Assigned');
    } 
    else{
    return redirect()->route('admin.user.assign.course')->with('status', 'Could not assign Course to some User');
    }  
}

我返回返回完整循环