未定义的偏移量:laravel中的2

when i try to save multiple records to database i get this error

Undefined offset: 2

My Store Controller

public function store(Request $request)
{
    $course = Course::create($request->all());

    $lessons = $request->except(['_token','_method']);
    for($x = 0;$x <= count($lessons); $x++)
    {
        $lesson = CourseClass::create([
            'course_id' => $course->id,
            'class_name' => $lessons['class_name'][$x],
            'class_desc' => $lessons['class_desc'][$x],
            'video' => $lessons['video'][$x],
            'files' => $lessons['files'][$x],
            'free' => $lessons['free'][$x],
        ]);
        $lesson->save();
    }
}
 for($x = 0;$x < count($lessons['class_name']); $x++)

Your index iterator is not compatible with your usage. You're getting a count of request params, and trying to use that to access the array values of each param.

$request->except(['_token','_method'])

probably returns something like:

[
  'class_name' => ['namea', 'nameb'],
  'class_desc' => ['desca', 'descb'],
  'video' => ['vida', 'vidb'],
  'files' => ['filesa', 'filesb'],
  'free' => [true, false],
]

So, count($lessons) will return (pretending my example is exactly accurate) 5. Whereas you're probably looking for 2 (the size of the sub arrays).

You could do as @tech suggests, and get the count from one of these sub arrays for your indexing instead:

count($lessons['class_name'])