Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

eggplantSword's avatar

When trying to create a item in foreach the item is not being created

I'm trying to do an import to the db and everything is working fine except when I try to create a group and group users, in the code what I'm doing is searching for a course and if the course exists then I add the course users (students) and I want to create a group also with the course users at the same time but the group part isn't working.

This is the code

DB::beginTransaction();
    foreach ($all as $a) {
        $item = array_values($a);

        $student = User::where('scard', $item[5])->first();
        $cname = str_replace('Descripción del Curso:                       ', '', $item[3]);
        $course = Course::where('name', $cname)->first();

        //this isn't working, nothing gets added to the db
        if (!empty($course)) {
            $group = Group::where('name', $period . ' ' . $course->name . ' ' . $category->name)->first();
            if (empty($group)) {
                $group = new Group();
                $group->name = $period . ' ' . $course->name . ' ' . $category->name;
                $group->user_id = auth()->user()->id;
                $group->course_id = $course->id;
                $group->major_id = $course->major_id;
                $group->period_id = $id;
                $class = Classroom::where('name', $course->classroom)->pluck('id')->first();
                $group->classroom_id = $class;
                $group->save();
            }
        }

        if (!empty($student) && !empty($course)) {
            $assign = CourseUsers::where('course_id', $course->id)
                ->where('period_id', $id)
                ->where('user_id', $student->id)
                ->where('user_type_id', UserType::$STUDENT)
                ->first();
            if (empty($assign)) {
                $assign = new CourseUsers();
                $assign->course_id = $course->id;
                $assign->period_id = $id;
                $assign->user_id = $student->id;
                $assign->user_type_id = UserType::$STUDENT;
                $assign->save();
            }

            //this isn't working, nothing gets added to the db
            $guser = GroupUsers::where('category_id', $category->id)
                ->where('group_id', $group->id)
                ->where('user_id', $student->id)
                ->first();
            if (empty($guser)) {
                $guser = new GroupUsers();
                $guser->category_id = $category->id;
                $guser->group_id = $group->id;
                $guser->user_id = $student->id;
                $guser->save();
            }
        }
    }
    DB::commit();

Why is this happening? How can I fix it?

0 likes
4 replies
m7vm7v's avatar

There is a lot that can get missed here.

If you dd($course); do you even get anything? The Group naming convention is not well lets say 'polished'. After getting it if you dd($group); do you see the expected result.

Also after the Descripción del Curso: there is some spacing, that looks somehow a random (maybe its not but...) is that correct at all?

Have you tried using this instead - $class = Classroom::where('name', $course->classroom)->first()->id;?

eggplantSword's avatar

@m7vm7v The spacing is deliberate, because this info is coming from an excel file and that's how the excel file has that column so I need to remove it for that field.

If I do a dd($group) inside the if(!empty($course) it never appears but that could be because that specific item doesn't have a course, and since it shows only the first item that doesn't really help to see much.

However I do know that when a course isn't empty it does execute the code in the if except for the GroupUsers part.

The ideal outcome is for each course that gets students or course users added to create a new group and set those same course users as the group users, I'm open to trying any other solution to achieve this.

m7vm7v's avatar

At that point you are already in a loop so if dd($group) never gets hit then $course = ... never returns a proper result... That can be only an assumption at this point, as it feels too conditional to get to that code. If you are confident that conditions and how you get the Models are correct then try for creating the group another approach ->

$group = Group::create([
  'name' => ...,
  'user_id' => auth()->user()->id,
  ...
]);

Also, have you tried what I proposed at first $class = Classroom::where('name', $course->classroom)->first()->id;

eggplantSword's avatar

@m7vm7v I haven't tried it, what makes this

$class = Classroom::where('name', $course->classroom)->first()->id;

different than this?

$class = Classroom::where('name', $course->classroom)->pluck('id')->first();

Please or to participate in this conversation.