msslgomez
2 months ago
216
3
Laravel

Check if item is inside a collection

Posted 2 months ago by msslgomez

I have a school which has majors which have courses, I need to check if a major belongs to a school. I also need to check if a course belongs to a school. I have a collection with the schools and the majors that belong to it, I can check if a course belong to a major through a fk major_id on the courses table.

This is what I currently have to check for if a major belongs to a school

//this is the collection with the schools and majors, I'll add only 3 
$ms = collect(
            [
                ['name' => 'Escuela de Enfermería',
                    'majors' => [
                        'Bachillerato en Enfermería',
                        'Licenciatura en Enfermería'
                    ]
                ],
                ['name' => 'Escuela de Psicología',
                    'majors' => [
                        'Licenciatura en Psicología'
                    ]
                ],
                ['name' => 'Escuela de Ingeniería de Sistemas',
                    'majors' => [
                        'Bachillerato en Ingeniería de Sistemas',
                        'Licenciatura en Ingeniería de Sistemas'
                    ]
                ],
            ]
}

//validate if they belong
if ($request->major && $request->school) {
            $major = Major::find($request->major);
            $ms->filter(function ($item) use ($request, $major) {
                if ($item['name'] == $request->school) {
                    foreach ($item['majors'] as $m) {
                        $d = Major::where('name', $m)->first();
                        if ($d != null) {
                            if ($d->id !== $request->major) {
                                throw ValidationException::withMessages(
                                    ['the major and school don't match']
                                );
                            }
                        }
                    }
                }
                return $item;
            });
        } else if ($request->course && $request->school) {
            $majorIds = collect([]);
            $ms->filter(function ($item) use ($request, $majorIds) {
                if ($item['name'] == $request->school) {
                    foreach ($item['majors'] as $m) {
                        $d = Major::where('name', $m)->first();
                        if (!empty($d)) {
                            $majorIds[] = $d->id;
                        }
                    }
                }
                return $item;
            });
            $courseIds = Course::whereIn('major_id', $majorIds)->pluck('id');

            foreach ($courseIds as $id) {
                if ($id !== $request->course) {
                    $course = Course::find($request->course);
                    throw ValidationException::withMessages(
                        ['the course and school don't match']
                    );
                }
            }
        }

This isn't working as expected and I'm not sure where my mistake is, how can I fix it?

Please sign in or create an account to participate in this conversation.