Laravel foreach仅获得第一价值

时间:2018-10-06 23:47:13

标签: laravel foreach

我正在做一个对等评分系统,它需要一个功能,该功能要求讲师添加ID列表,当学生报名参加课程时,他输入所需的ID以与讲师ID列表上的ID相匹配。

控制器

public function store(Request $request)
{
    $this->validate($request, [
        'course_code' => 'required',
        'studentid' => 'required'
    ]);

    $enrollment = new Enrollment;
    $enrollment->user_id = auth()->user()->id;
    $enrollment->course_id = $request->course;
    $enrollment->user_StudentID = $request->studentid;

    $input_course_id = $request->input('course_code');
    $input_studentid = $request->input('studentid');
    $course = Course::find($enrollment->course_id);

    $course_identifiers = $course->identifiers;

    // Need all the data in the database course table for comparison
    //$course represents the contents of the course table in all databases, then you need to loop first, then judge
    //$course stands for list $signleCourse for each piece of data

    foreach ($course_identifiers as $course_identifier) {
        //              if ($course_identifier->studentid == $input_studentid )
        if ($input_studentid == $course_identifier->studentid) {
            if ($request->course == $input_course_id) {
                //if true,save and redirect
                $enrollment->save();
                return redirect('/enrollment')->with('success', 'Course Enrolled');
            } else {
                return redirect('/enrollment')->with('error', 'Please Enter Correct Confirmation Code');
                //If false do nothing
            }
        } else {
            return redirect('/enrollment')->with('error', 'Please Enter Correct Student ID');
            //If false do nothing
        }
    }
}

它只能匹配第一个值,但我输入的其他值无法识别。

1 个答案:

答案 0 :(得分:1)

关闭重定向。确实很难理解该代码的上下文,但是看起来如果它与匹配项不匹配,则会重定向,因此不会遍历$ course_identifiers的第二个和后续值。